我的PDO语法中的错误是什么

时间:2013-05-02 20:42:43

标签: php mysql pdo

抱歉,我无法想出一个更好的头衔。 : - (

这是我第一次尝试使用PDO。我已经引用了php.net的语法。 数据库为空,如下所示:

Field         Type       Null   Key Default Extra
id            int(11)    NO     PRI NULL    auto_increment
query_string  text       NO         NULL    
exclude_count tinyint(4) NO         NULL    

运行代码应该在pages表中创建或更新一行,DB不变。

这是我的代码和输出:

$dbuser='abc';
$dbpass='password';

$connection = new PDO('mysql:host=127.0.0.1;dbname=mydb', $dbuser, $dbpass);
if(!$connection){echo '<!-- DB CONNECTION ERROR -->';}else{echo '<!-- DB CONNECTION CONFIRMED -->';}
$connection->beginTransaction();
$prep_idcheck=$connection->prepare("SELECT id FROM pages WHERE query_string = :origqs");
$prep_idcheck->bindParam(':origqs', $orig_QS);
$row=$prep_idcheck->fetch();

if($row)
  {
    echo '<!-- EXDB: We have dealt with this one before. -->';
    $existing=true;
  }
else
  {
    echo '<!-- EXDB: This is a new one. -->';
    $existing=false;
  }

if($existing)
  {
    $prep_report_excounts=$connection->prepare("UPDATE pages SET exclude_count = :exclude_count WHERE query_string = :origqs");
  }
else
  {
    $prep_report_excounts=$connection->prepare("INSERT INTO pages (exclude_count, query_string) VALUES (:exclude_count,:origqs)");
  }
$prep_report_excounts->bindParam(':origqs', $orig_QS);
$prep_report_excounts->bindParam(':exclude_count', $exclude_count);
$status=$prep_report_excounts->execute();
if(!$status)
  {
   $error=$connection->errorInfo();
    echo'<!-- The statement did not execute '."\n";
    var_dump($error);
    echo'-->';
  }
$connection->commit();
?>

输出

<!-- DB CONNECTION CONFIRMED --><!-- EXDB: This is a new one. --><!-- The statement did not execute 
array(1) {
  [0]=>
  string(5) "00000"
}
-->

2 个答案:

答案 0 :(得分:5)

您错过了execute()

上的$prep_idcheck
$prep_idcheck=$connection->prepare("SELECT id FROM pages WHERE query_string = :origqs");
$prep_idcheck->bindParam(':origqs', $orig_QS);
// Missing execute();
$prep_idcheck->execute();
$row=$prep_idcheck->fetch();

答案 1 :(得分:4)

你没有执行你的陈述

$prep_idcheck->bindParam(':origqs', $orig_QS);
$prep_idcheck->execute(); // <------