2
PostgreSQL escape functions
4
<?php include("skipif.inc"); ?>
9
define('FILE_NAME', dirname(__FILE__) . '/php.gif');
11
// pg_escape_string() test
12
$before = "ABC\\ABC\'";
13
$expect = "ABC\\\\ABC\\'";
14
$after = pg_escape_string($before);
15
if ($expect === $after) {
16
echo "pg_escape_string() is Ok\n";
19
echo "pg_escape_string() is NOT Ok\n";
25
// pg_escape_bytea() test
27
$expect = "ABC\\\\\\\\ABC";
28
$after = pg_escape_bytea($before);
29
if ($expect === $after) {
30
echo "pg_escape_bytea() is Ok\n";
33
echo "pg_escape_byte() is NOT Ok\n";
39
// Test using database
40
$data = file_get_contents(FILE_NAME);
41
$db = pg_connect($conn_str);
43
// Insert binary to DB
44
$escaped_data = pg_escape_bytea($data);
45
pg_query("DELETE FROM ".$table_name." WHERE num = -9999;");
46
$sql = "INSERT INTO ".$table_name." (num, bin) VALUES (-9999, CAST ('".$escaped_data."' AS BYTEA));";
49
// Retrieve binary from DB
50
$sql = "SELECT bin::bytea FROM ".$table_name." WHERE num = -9999";
51
$result = pg_query($db, $sql);
52
$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
54
if ($data === pg_unescape_bytea($row['bin'])) {
55
echo "pg_escape_bytea() actually works with database\n";
58
echo "pg_escape_bytea() is broken\n";
63
pg_escape_string() is NOT Ok
65
string(12) "ABC\\ABC\\''"
66
string(10) "ABC\\ABC\'"
67
pg_escape_bytea() is Ok
68
pg_escape_bytea() actually works with database