|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-06-16 22:47 UTC] daniel dot baulig at gmx dot de
Description:
------------
If you call bindColumn on a non existent column it will always create a warning, no matter how the PDO object is configured to respond to errors (ERRMODE_EXCEPTION, ERRMODE_SILENT, ERRMODE_WARNING).
I actually recreated this on PHP 5.3.1 and PHP 5.2.6-1+lenny8 (from Debian Lenny repository) and have NOT tried it on 5.3.2. Since I was not able to find any references for this bug on the web, I believe I might be the first one to encounter it and it is propably not fixed as of PHP 5.3.2
Test script:
---------------
$pdo = new PDO('mysql:host=localhost;dbname=db', 'root', '');
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$statement = $pdo->prepare('SELECT id FROM aTable');
$statement->execute();
$field= NULL;
try
{
$statement->bindColumn('none_existent_field', $field, PDO::PARAM_INT);
while ($statement->fetch( PDO::FETCH_BOUND ))
{
echo $field. "<br/>\n";
}
}
catch (PDOException $e)
{
echo "Database field not in result set!<br/>\n";
}
Expected result:
----------------
The string "Database field not in result set!<br/>\n";
Actual result:
--------------
A warning:
<b>Warning</b>: PDOStatement::bindColumn() [<a href='pdostatement.bindcolumn'>pdostatement.bindcolumn</a>]: Did not found column name 'none_existent_field' in the defined columns; it will not be bound in <b>/path/to/script.php</b> on line <b>10</b><br />
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 20:00:01 2025 UTC |
After thinking a bit about it I came to the conclusion this might be a feature, not a bug. If you are handed a PDOStatement from some other source (a library or a callee) you might not know which exact columns are in the result set. Because there also is no way to look them up you are stuck with "brute force" binding each column, that might be in that result set. If this would throw an Exception you would need to try { } catch ( PDOException $e ) { } every single bindColumn. While I do think this would be the correct approach from a design standpoint it is very clunky and cumbersome actually programming. If this method only issues a warning then you could simply @ supress that warning and try bindColumn on whatever you like. However, if this behaviour remains it should be documentet clearly in the PHP PDO manual, because it is not what you'd expect.