3

抱歉,我想不出更好的标题。:-(

这是我第一次尝试使用 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"
}
-->
4

2 回答 2

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();
于 2013-05-02T20:47:48.790 回答
4

你没有执行你的声明

$prep_idcheck->bindParam(':origqs', $orig_QS);
$prep_idcheck->execute(); // <------
于 2013-05-02T20:47:40.843 回答