1

Trying to move to PDO can't figure out what is wrong with this

$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
$sql = "SELECT COUNT(*) FROM users WHERE repo=? AND list=? AND email=?";
$q=$dbh->prepare($sql);
$res = $q->execute(array($repo, $list, $email));
$v = $res->fetchColumn();
mcktimo
  • 1,440
  • 4
  • 19
  • 35
  • 1
    [How to squeeze error message out of PDO?](http://stackoverflow.com/q/3726505) – Pekka Jan 20 '12 at 21:38
  • 1
    possible duplicate of [Error in my PDO fetch column function](http://stackoverflow.com/questions/4407413/error-in-my-pdo-fetch-column-function) – outis Jan 21 '12 at 18:58

2 Answers2

1

PDOStatement::execute() returns either TRUE or FALSE (see here), so $res is being set to a boolean and then you're trying to call a method on it. Try this instead:

$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
$sql = "SELECT COUNT(*) FROM users WHERE repo=? AND list=? AND email=?";
$q=$dbh->prepare($sql);
if (($res = $q->execute(array($repo, $list, $email))) === FALSE) {
    echo 'Query failed!';
    exit;
}
$v = $q->fetchColumn();
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
  • bool PDOStatement::execute ([ array $input_parameters ] ) it doesn't return mixed value, only boolean value. – WojtekT Jan 20 '12 at 21:43
  • (trying to make a note on this)So the $dbh is an database object $q is a statement object which, once executed, has the resultset hung on it? – mcktimo Jan 20 '12 at 22:03
0
$q->execute(...)

Returns only boolean value. Proper way:

$q = $dbh->prepare($sql);

if ($q->execute($repo, $list, $email) == TRUE) {
 $v = $q->fetchColumn();
} else {
 //error
}
WojtekT
  • 4,735
  • 25
  • 37