Wednesday, 21 August 2013

PDO Prepared Statement returns empty

PDO Prepared Statement returns empty

I have wrapped a fetchAll function for PDO using MySQL like this:
function fetchAll($query, $values = null){
global $db;
$sth = $db->prepare($query);
var_dump($sth);
$a = $sth->execute($values);
var_dump($a);
$values = $sth->fetchAll();
return $values;
}
It just returns all values as an array, to simplify. It works on the
following querries perfectly:
fetchAll("INSERT INTO course(dept, no, name, semester, section, teacher,
students, gpa) VALUES(?, ?,?,?,?,?,?,?)", array($dept, $no, $name,
$semester, $section, $teacher, $students, $gpa));
fetchAll("SELECT DISTINCT(no), name FROM course WHERE dept = ? ORDER BY
no", array($deptCode));
But problem is in this query, it does not give any error (I enabled tehe
exceptions):
fetchAll("SELECT teacher, semester FROM course WHERE dept = 'CS' AND no =
?", array(101)); // does not work
However the following works:
fetchAll("SELECT teacher, semester FROM course WHERE dept = 'CS' AND no =
101");
Why my last query does not work when I pass it as an array? I also tried
associative array bingind, but it did not work also.

No comments:

Post a Comment