Call to undefined method PDO execute

时间:2015-06-30 13:52:49

标签: php pdo

I've get this error on the page. What can be the problem.. I've checked tons of thread here with the same problem and can't figured it out.

$ip_address = $_SERVER['REMOTE_ADDR'];
$vote_rank = 0;
$query = $pdo->prepare("SELECT SUM(vote_rank) as vote_rank FROM ipaddress_vote_map WHERE image_id = ? and ip_address = ?");
$query -> bindParam(1, $_GET['image_id'], PDO::PARAM_INT);
$query -> bindParam(1, $ip_address, PDO::PARAM_INT);                          
$rowsa = $pdo->execute();
$up = "";
$down = "";

if(!empty($rowsa[0]["vote_rank"])) {
   $vote_rank = $row[0]["vote_rank"];
   if($vote_rank == -1) {
       $up = "enabled";
       $down = "disabled";
   }
if($vote_rank == 1) {
       $up = "disabled";
       $down = "enabled";
}
}

2 个答案:

答案 0 :(得分:1)

Prepare returns the object you need to execute so your code should be:

$stmt= $pdo->prepare("SELECT SUM(vote_rank) as vote_rank FROM ipaddress_vote_map WHERE image_id = ? and ip_address = ?");
$stmt-> bindParam(1, $_GET['image_id'], PDO::PARAM_INT);
$stmt-> bindParam(2, $ip_address, PDO::PARAM_INT);                          
$rowsa = $stmt->execute();

答案 1 :(得分:0)

execute is a method of the PDOStatement class (see docs), not of PDO. The return value of PDO::prepare is an instance of PDOStatement, so replace $pdo->execute(); with $query->execute();

The bindParam calls also seem incorrect to me, as the docs say on the first argument ($parameter):

Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.

This means that this:

$query -> bindParam(1, $_GET['image_id'], PDO::PARAM_INT);
$query -> bindParam(1, $ip_address, PDO::PARAM_INT);

Should be:

$query->bindParam(1, $_GET['image_id'], PDO::PARAM_INT);
$query->bindParam(2, $ip_address, PDO::PARAM_INT);