note 105290 added to function.pg-escape-string
| From: | strata_ranger at hotmail dot com | Date: | Mon, 08 Aug 2011 03:20:56 +0000 |
| Subject: | note 105290 added to function.pg-escape-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182307@lists.php.net to get a copy of this message | ||
This may seem obvious, but remember that pg_escape_string escapes values for use as string literals
in an SQL query -- if you need to escape arbitrary strings for use as SQL identifiers (column names,
etc.), there doesn't seem to be a PHP function for that so you'll have to do that escaping
yourself. (PostgreSQL has an in-database function, quote_ident(), that does this.)
This can be an issue if your database contains mixed-case (or otherwise unusual) column names and
you have a class interface managing your database/query interactions (for connecting to different
types of databases). If you don't double-quote your column names then postgreSQL will match
them case-insensitively, but will label the results in all-lowercase (which differs from MySQL).
For example:
<?php
// Plain column identifier
$res = pg_query("Select columnName from table");
$row = pg_fetch_assoc($res);
var_dump($row['columnName']); // Doesn't work (throws E_NOTICE)
var_dump($row['columnname']); // Works
// Escaped column identifier
$res = pg_query("Select \"columnName\" from table");
$row = pg_fetch_assoc($res);
var_dump($row['columnName']); // Works
var_dump($row['columnname']); // Doesn't
?>
----
Server IP: 69.147.83.197
Probable Submitter: 76.2.22.129
----
Manual Page -- http://www.php.net/manual/en/function.pg-escape-string.php
Edit -- https://master.php.net/note/edit/105290
Del: integrated -- https://master.php.net/note/delete/105290/integrated
Del: useless -- https://master.php.net/note/delete/105290/useless
Del: bad code -- https://master.php.net/note/delete/105290/bad+code
Del: spam -- https://master.php.net/note/delete/105290/spam
Del: non-english -- https://master.php.net/note/delete/105290/non-english
Del: in docs -- https://master.php.net/note/delete/105290/in+docs
Del: other reasons-- https://master.php.net/note/delete/105290
Reject -- https://master.php.net/note/reject/105290
Search -- https://master.php.net/manage/user-notes.php