简体   繁体   English

SQLSTATE [HY000]:一般错误和array(0){}

[英]SQLSTATE[HY000]: General error and array(0){}

In my pdo script, I was querying my database to give me all the users in my database in my local machine but all I see is "SQLSTATE[HY000]: General error" 在我的pdo脚本中,我正在查询数据库以给我本地计算机上数据库中的所有用户,但是我看到的只是“ SQLSTATE [HY000]:常规错误”

Here is my script; 这是我的剧本;

<?php
ini_set('display_errors','on');
require_once('connect.php');

try{

$username='charlyo';
$con=$connect->query('select * from users where username=:username');
var_dump($con->fetchAll());
}
catch(PDOException $e){
   echo $e->getMessage();

}

 ?>

and while trouble-shooting it I tried to use prepared statements. 在解决它时,我尝试使用准备好的语句。 I now get "array(0){}" without returning any arrays while I used fetchAll method. 现在,当我使用fetchAll方法时,我没有返回任何数组就得到了“ array(0){}”。

Here is the second script of prepared statement: 这是prepared语句的第二个脚本:

<?php
ini_set('display_errors','on');
require_once('connect.php');

try{

$username='charlyo';
$con=$connect->prepare('select * from users where username=:username');
$con->bindParam(':username',$username);
$con->execute();
var_dump($con->fetchAll());
}
catch(PDOException $e){
   echo $e->getMessage();

}

 ?>

You should also post the contents of the file connect.php , so we can be sure of how you are instantiating the PDO object (make sure to remove the passwords). 您还应该发布文件connect.php的内容,以便我们可以确定如何实例化PDO对象(请确保删除密码)。

Stacked queries (multiples queries separated by a ; ) are not supported by default in PDO, the emulation mode has to be enabled ( PDO::ATTR_EMULATE_PREPARES ) but it's not really needed in your case. 在PDO中,默认情况下不支持堆叠查询(多个查询,用;分隔),必须启用仿真模式( PDO::ATTR_EMULATE_PREPARES ),但在您的情况下确实不需要。

In order to make your second example work, you should just remove the use charles; 为了使第二个示例正常工作,您应该删除use charles; part and make sure to pass dbname=charles in the DSN that you are providing to PDO's constructor. 部分,并确保将您提供的DSN中的dbname=charles传递给PDO的构造函数。

Remove use charles; 删除use charles; from query. 从查询。 I am not sure why you have used it but take it off from query and check. 我不确定您为什么使用它,但是将其从查询和检查中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM