note 90981 added to function.pg-fetch-assoc
| From: | Anonymous at osu1 dot php dot net | Date: | Tue, 19 May 2009 15:24:27 +0000 |
| Subject: | note 90981 added to function.pg-fetch-assoc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-155748@lists.php.net to get a copy of this message | ||
Beware! If your query returns multiple columns with the same names, only the right-most one will be
contained in the result array. This can cause problems if you are using a combination of joins:
For example:
<?php
// Let's say that 'pkey' is the primary-key colum for tables a and b (primary keys
are never null)
$res = pg_query("Select a.pkey, b.* FROM a LEFT JOIN b using (pkey)");
$data = pg_fetch_assoc($res);
var_dump($data['pkey']) // Is actually b.pkey, may be NULL!
?>
Both tables contain a column named 'pkey'. Now table 'b' is on the optional
side of a LEFT JOIN, so b.pkey (implicitly included via 'b.*') may be NULL.
The problem arises when you use pg_fetch_assoc(), there are two columns named 'pkey' but
the result array can only contain one value per key -- in this case it will pick the one from table
B instead of the one from table A, and since B is on the optional side of the left-join,
$data['pkey'] may be NULL. So if you're expecting to retrieve the column from table
A, you need to use a different pg_fetch() or rewrite your query to avoid ambiguity.
----
Server IP: 69.147.83.197
Probable Submitter: 76.2.25.104
----
Manual Page -- http://www.php.net/manual/en/function.pg-fetch-assoc.php
Edit -- https://master.php.net/note/edit/90981
Del: integrated -- https://master.php.net/note/delete/90981/integrated
Del: useless -- https://master.php.net/note/delete/90981/useless
Del: bad code -- https://master.php.net/note/delete/90981/bad+code
Del: spam -- https://master.php.net/note/delete/90981/spam
Del: non-english -- https://master.php.net/note/delete/90981/non-english
Del: in docs -- https://master.php.net/note/delete/90981/in+docs
Del: other reasons-- https://master.php.net/note/delete/90981
Reject -- https://master.php.net/note/reject/90981
Search -- https://master.php.net/manage/user-notes.php