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"
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.
Here is the second script of prepared statement:
<?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).
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.
In order to make your second example work, you should just remove the use charles;
part and make sure to pass dbname=charles
in the DSN that you are providing to PDO's constructor.
Remove use charles;
from query. I am not sure why you have used it but take it off from query and check.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.