我对PDO mysql查询有问题

这是我的简单代码(不是为了显示问题的实际情况)

$connexion = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
$stmt = $connexion->prepare('SELECT * from missions,platforms WHERE missions_platforms_id = exl_platforms_id AND missions_id = :mission');
$stmt->execute(array(":mission" => '1'));
$ligne = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($stmt);

$stmt = $connexion->prepare('SELECT * from missions,platforms WHERE missions_platforms_id = exl_platforms_id AND missions_id = :mission');
var_dump($stmt);
$stmt = $connexion->prepare('SELECT * from missions,platforms WHERE missions_platforms_id = exl_platforms_id AND missions_id = :mission');
var_dump($stmt);


和输出:

object(PDOStatement)[2]
 public 'queryString' => string 'SELECT * from missions,platforms WHERE missions_platforms_id = platforms_id AND missions_id = :mission' (length=122)

boolean false

object(PDOStatement)[2]
 public 'queryString' => string 'SELECT * from missions,platforms WHERE missions_platforms_id = platforms_id AND missions_id = :mission' (length=122)


为什么第二个var_dump返回false?

如果我多次执行相同的模式,则一半时间返回false。

谢谢

最佳答案

使用PDOStatement::fetch()使用PDOStatement::closeCursor()读取结果集后,尝试关闭结果集游标,例如:

$connexion = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
$stmt = $connexion->prepare('SELECT * from missions,platforms WHERE missions_platforms_id = exl_platforms_id AND missions_id = :mission');
$stmt->execute(array(":mission" => '1'));
$ligne = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($stmt);
$stmt->closeCursor();

$stmt = $connexion->prepare('SELECT * from missions,platforms WHERE missions_platforms_id = exl_platforms_id AND missions_id = :mission');
var_dump($stmt);
$stmt = $connexion->prepare('SELECT * from missions,platforms WHERE missions_platforms_id = exl_platforms_id AND missions_id = :mission');
var_dump($stmt);


第一条语句是您实际从中获取结果的唯一语句。

关于php - PDO查询一半时间返回False,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6596871/

10-12 06:20