ZF-2112: Zend_Db_Adapter_Interface::fetchCol() does not properly retrieve a column on single column SQL statements.
Description
When using a Mysqli adapter(not sure if this occurs with others) the following produces an error:
Warning: mysqli_stmt::bind_param() [function.mysqli-stmt-bind-param]: Number of variables doesn't match number of parameters in prepared statement in /usr/home/sciworld/application/library/Zend/Db/Statement/Mysqli.php on line 241
This occurs independent of if a column index is provided.
Interestingly, the following code does work correctly:
$stmnt = $db->query("SELECT areaID FROM userPermissions WHERE userID='$userID'"); $column = $stmnt->fetchColumn();```
It seems perhaps the parsing of the statement in the first case is not functioning properly, although admittedly I don't have much experience with Zend_Db. I do know the documentation does indicate that the first case should work identically to the second, which is not the case(unless I am missing something here).
Comments
Posted by Wil Sinclair (wil) on 2008-03-21T17:05:34.000+0000
This issue should have been fixed for the 1.5 release.
Posted by Wil Sinclair (wil) on 2008-03-25T20:43:59.000+0000
Please categorize/fix as needed.
Posted by Wil Sinclair (wil) on 2008-04-18T13:11:54.000+0000
This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.
Posted by Simon Mundy (peptolab) on 2008-05-26T18:50:48.000+0000
What's the final SQL query? What does $userID resolve to? I can't reproduce this here but if you can supply any additional information it will help immensely.
Posted by Cole Snodgrass (cole.snodgrass) on 2008-06-25T12:44:12.000+0000
It appears that this is still happening (verified in 1.5.2) if the where statement is searching for a column with an id of 0 and the column being returned has a 0 in it.
Repro:
Expected Result:
Actual Result:
Notice that the first fetchCol is returning an empty arrays instead of the expected results.
Posted by Cole Snodgrass (cole.snodgrass) on 2008-06-25T13:10:29.000+0000
I traced this issue down to the Zend_Db_Statement::fetchAll function and determined this is due to improper type checking.
Notice that both {{while}} loops are not checking the types returned from {{fetchColumn}} and {{fetch}}, therefore anything that could evaluate to a false will cause the {{while}} loop to terminate. This is why when the integer 0 is being returned by the {{fetchColumn}} method, nothing is being returned.
Updating the code to verify that a boolean false was returned appears to fix this issue (I can only vouch that this fixes the fetchCol issue, I didn't bother to look into what {{fetch}} should/could return so that should still be verified).
Posted by Wil Sinclair (wil) on 2009-01-06T14:33:40.000+0000
This issue has gone unaddressed for too long. I'm re-assigning to Ralph for re-evaluation and categorization.
Posted by Ralph Schindler (ralph) on 2009-01-10T10:22:48.000+0000
Will evaluate within 2 weeks
Posted by Tomasz Gunerski (tommygun) on 2009-01-21T20:05:16.000+0000
Cole Snodgrass solution is incorrect for Mysqli adapter. False is returned only when error will occurre, when stmt cursor reaches the end of data NULL will be returned. More about this here: http://php.net/manual/en/mysqli-stmt.fetch.php
Proper solution:
Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2009-05-16T18:02:42.000+0000
can not reproduce
Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2009-05-29T07:25:01.000+0000
Reopen for ZF-6788
Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2009-05-29T07:59:06.000+0000
add "false !== " condition to "$val = $this->fetchColumn($col)".
But I think the problem with mysql should be issue on fetch() method.