准备好的语句得到错误0

时间:2014-10-12 09:53:57

标签: php mysqli errno prepare


尝试制作一个准备语句,但由于某种原因,它失败了我,我得到错误(文本)为空的错误0。是什么造成的?现在已经在网上搜索了一段时间。

<?php 
   $dbh = new mysqli("localhost","root","","honeypot");

   if ($dbh->connect_errno) {
       echo "Connection failed: (" . $dbh->connect_errno . ") " . $dbh->connect_error;  
       die();
   }

   //Prepare
   if (!($stmt = $dbh->prepare("SELECT tblUsers WHERE UserName = ?"))) {
       echo "Prepare failed: (" . $dbh->connect_errno . ") " . $dbh->connect_error;
   }
?>

1 个答案:

答案 0 :(得分:0)

您正在获取error 0,因为您正在打印$dbh->connect_error,但您在创建连接时没有错误。对于初始连接以外的所有内容,您应使用$dbh->error

echo "Prepare failed: (" . $dbh->errno . ") " . $dbh->error;

您收到错误,因为您的查询存在语法错误。它应该是:

SELECT col1, col2, col3, ... FROM tblUsers WHERE UserName = ?

您错过了列列表和FROM关键字。