我刚刚开始学习大量的PDO和准备好的语句(每次都记得要记住使用mysql_real_escape_string())但是我无法让脚本正常执行:
<?php
error_reporting(E_ALL);
echo "start";
try{
$dbh=new PDO('mysql:host=localhost;dbname=DBNAME','USER','PWD');
}
catch(PDOException $e){
echo 'Error connecting to MySQL!: '.$e->getMessage();
exit();
}
$dbh->prepare('SELECT * FROM users WHERE uid= ?');
$dbh->execute(array('15400743'));
$result=$dbh->fetchAll();
print_r($result);
echo "end";
?>
这几乎是从示例代码中复制而来,但执行时只返回“start”。我已经仔细检查了我的db / user / pw。别人看错了吗?谢谢!
答案 0 :(得分:6)
正确的方法是:
<?php
error_reporting(E_ALL);
echo "start";
try{
$dbh=new PDO('mysql:host=localhost;dbname=DBNAME','USER','PWD');
}
catch(PDOException $e){
echo 'Error connecting to MySQL!: '.$e->getMessage();
exit();
}
$stmt = $dbh->prepare('SELECT * FROM users WHERE uid= ?');
$stmt->execute(array('15400743'));
$result = $stmt->fetchAll();
print_r($result);
echo "end";
?>
请注意为$ stmt变量分配准备以及之后的使用。