Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohgaki/1387033 to your computer and use it in GitHub Desktop.
Save yohgaki/1387033 to your computer and use it in GitHub Desktop.
PHP: Example code for pgsql module with pg_escape_identifier()/pg_escape_literal() patch
<?php
$db = pg_connect('host=127.0.0.1 dbname=test');
// Create test table with reserved word "user"
$sql = 'drop table test2; create table test2 ('.pg_escape_identifier('user').' text);';
var_dump($sql);
pg_query($sql);
// Example use of pg_escape_literal().
// Note that no quotes around string. Quotes are added by the function.
// "user" is reserved, so pg_escape_identifier() is required.
$sql = 'insert into test2 ('.pg_escape_identifier('user').') values ('.pg_escape_literal('root').');';
var_dump($sql);
pg_query($sql);
// See if this is working
$res = pg_query('select * from test2');
var_dump(pg_fetch_all($res));
/* Output with PostgreSQL 8.4 as server
[yohgaki@dev php-src]$ ./php pg_test.php
string(51) "drop table test2; create table test2 ("user" text);"
string(45) "insert into test2 ("user") values ( E'root');"
array(1) {
[0]=>
array(1) {
["user"]=>
string(4) "root"
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment