当我运行此代码时,它不会显示查询的任何结果

时间:2014-02-02 16:02:37

标签: php database pdo

查询不会产生任何结果。变量响应应该保存查询结果,但是当我测试它包含使用var_dump时,它表明它是空的。

 try
 {
    $bdd = new PDO('mysql:host=localhost;dbname=ems', 'root', '');
 } catch(Exception $e)
 {
    die('Erreur : '.$e->getMessage());
 }
 $code=123456;

 $query = $bdd->prepare('SELECT Code,sender FROM info WHERE Code = ?');
 //,reciever,origin,destination,transit
 $query->execute(array($code));
 if($query->fetchColumn())
 {
     echo"good";
     while($reponse = $query->fetch()){
         var_dump($reponse);

         echo"good";
         echo $reponse['sender'];
     }

   } else{
     echo" The Code entered  was not  found please verify and  enter again";
 }

1 个答案:

答案 0 :(得分:0)

我不确定这是否只是代码中的拼写错误,但我注意到了这段代码:

if($query->fetchColumn())
{
    echo"good";
    while($reponse = $query->fetch()){
     var_dump($reponse);

     echo"good";
     echo $reponse['sender'];
    }

else{
 echo" The Code entered  was not  found please verify and  enter again";
}
}

查看您的代码(以及您的评论),{语句有一个开括号if,后跟while循环的开括号。然后有一个闭合支撑。右括号}用于while循环。然后你有一对else块的大括号。在}阻止之后,您还有另一个else。尝试在}阻止之前添加else,并在}阻止后移除else之一。 else块不应嵌套在if块中(如果我完全理解你的话)。这是语法错误。

代码的外观如何:

if($query->fetchColumn())
{
    echo"good";
    while($reponse = $query->fetch()){
     var_dump($reponse);

     echo"good";
     echo $reponse['sender'];
    }
}
else{
 echo" The Code entered  was not  found please verify and  enter again";

}