PDO查询返回False的一半时间

时间:2011-07-06 13:07:38

标签: php mysql pdo prepare

我有PDO mysql查询的问题

HEre是我的简单代码(不是真实情况只是为了向您展示问题)

$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 ...

谢谢你

1 个答案:

答案 0 :(得分:2)

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

$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);

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