-2

When i run this query in phpmyadmin it runs fine and returns what it should return.
When i run it in php with PDO it fails. Can someone tell me why? The error message shows that the right parameters were used.

Code i run in phpmyadmin.

SELECT  `fd1`.`id` ,  `fd2`.`id` ,  `fd3`.`id` 
FROM  `food` AS  `fd1` ,  `food` AS  `fd2` ,  `food` AS  `fd3` 
WHERE  `fd1`.`food` =  'Empty'
AND  `fd2`.`food` =  'Carrot'
AND  `fd3`.`food` =  'Empty'

Code i run in php

$sql = 'SELECT  `fd1`.`id` ,  `fd2`.`id` ,  `fd3`.`id` 
        FROM  `food` AS  `fd1` ,  `food` AS  `fd2` ,  `food` AS  `fd3` 
        WHERE  `fd1`.`food` = ? 
        AND  `fd2`.`food` = ? 
        AND  `fd3`.`food` = ?'; 
$stmt = $db->prepare($sql);
$stmt->bindParam(1, $food1, PDO::PARAM_STR);
$stmt->bindParam(2, $food2, PDO::PARAM_STR);
$stmt->bindParam(3, $food3, PDO::PARAM_STR);
$stmt->execute();


if (!$stmt->rowCount())
    die("ERROR:FOOD_SEARCH_FAILURE food:$food1, $food2, $food3 rows returned". $stmt->rowCount());

I also tried to run it with:

$stmt->execute(array($food1, $food2, $food3)); 

Same results

Not Amused
  • 942
  • 2
  • 10
  • 28

2 Answers2

2

rowCount() does not return the number of rows affected by a SELECT statement for most databases
Manual

david strachan
  • 7,174
  • 2
  • 23
  • 33
  • For mysql it could be an issue only in case of ancient version of PDO. – Your Common Sense Apr 07 '13 at 15:12
  • @YourCommonSense I didn't quite understand why it should be an issue only for ancient versions? Obviously it is always an issue, since `rowCount` returns the number of affected (deleted/updated/inserted but not selected) rows! – agim Apr 08 '13 at 12:17
1

First, connect to PDO the way described in the PDO tag wiki and set error reporting on to be sure you will see any error occurred.
Make an intentional error in the query to test if you can see them.

Next, if no errors anyway - check your data, both in database and bound variables (there is nowhere in your code you define them)

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345