php-src/ext/pgsql/tests/escape.inc

60 lines
1.4 KiB
PHP
Raw Normal View History

2002-04-04 13:59:44 +00:00
<?php
2002-04-05 06:05:28 +00:00
include 'config.inc';
define('FILE_NAME', './php.gif');
// pg_escape_string() test
$before = "ABC\\ABC\'";
$expect = "ABC\\\\ABC\\'";
2002-04-04 13:59:44 +00:00
$after = pg_escape_string($before);
if ($expect === $after) {
2002-04-04 15:25:46 +00:00
echo "pg_escape_string() is Ok\n";
2002-04-04 13:59:44 +00:00
}
else {
2002-04-04 15:25:46 +00:00
echo "pg_escape_string() is NOT Ok\n";
2002-04-04 13:59:44 +00:00
var_dump($before);
var_dump($after);
var_dump($expect);
}
2002-04-05 06:05:28 +00:00
// pg_escape_bytea() test
2002-04-04 13:59:44 +00:00
$before = "ABC\\ABC";
$expect = "ABC\\\\\\\\ABC";
$after = pg_escape_bytea($before);
if ($expect === $after) {
2002-04-04 15:25:46 +00:00
echo "pg_escape_bytea() is Ok\n";
2002-04-04 13:59:44 +00:00
}
else {
2002-04-04 15:25:46 +00:00
echo "pg_escape_byte() is NOT Ok\n";
2002-04-04 13:59:44 +00:00
var_dump($before);
var_dump($after);
var_dump($expect);
}
2002-04-05 06:05:28 +00:00
// Test using database
2002-04-19 12:24:58 +00:00
$fp = fopen(FILE_NAME,'r');
2002-04-05 06:05:28 +00:00
$data = fread($fp, filesize(FILE_NAME));
2002-04-19 12:24:58 +00:00
$db = pg_connect($conn_str);
2002-04-05 06:05:28 +00:00
2002-04-19 12:24:58 +00:00
// Insert binary to DB
2002-04-05 06:05:28 +00:00
$escaped_data = pg_escape_bytea($data);
2002-04-19 12:24:58 +00:00
pg_query("DELETE FROM ".$table_name." WHERE num = -9999;");
$sql = "INSERT INTO ".$table_name." (num, bin) VALUES (-9999, CAST ('".$escaped_data."' AS BYTEA));";
2002-04-05 06:05:28 +00:00
pg_query($db, $sql);
2002-04-19 12:24:58 +00:00
// Retrieve binary from DB
$sql = "SELECT bin::bytea FROM ".$table_name." WHERE num = -9999";
2002-04-05 06:05:28 +00:00
$result = pg_query($db, $sql);
2002-04-19 12:24:58 +00:00
$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
// Compare
// Need to wait PostgreSQL 7.3.x for PQunescapeBytea()
// if ($data === pg_unescape_bytea($row['bin'])) {
// echo "pg_escape_bytea() actually works with databse\n";
// }
// else {
// echo "pg_escape_bytea() is broken\n";
// }
2002-04-05 06:05:28 +00:00
2002-04-04 15:25:46 +00:00
?>