Problem with PDO request
I am updating some code from old mysql_ * functions to PDO. It connects without issue, runs the query without issue, but the result set is empty. PDO :: query () should return a PDOStatement object, but I'm coming back. No errors reported.
Here is my code:
try
{
$DB = new PDO("mysql:host=localhost;dbname=dbname", "user", "pass");
$stmt = $DB->prepare("SELECT * FROM report_clientinfo");
$stmt->execute();
}catch(PDOException $e)
{
echo $e->getMessage() . "\n";
}
echo gettype($stmt) . "\n";
if ($stmt) echo "true\n";
else echo "false\n";
$resultset = $stmt->fetchAll();
if(empty($resultset))
{
exit("ERROR: getClientInfo query failed.");
}
$DB = null;
print_r($resultset);
The output I see is:
object true ERROR: getClientInfo request failed.
Any ideas why it is not returning any results?
source to share
object
true
ERROR: getClientInfo query failed.
It seems to me that your variable PDOStatement $stmt
is actually considered an object and not " true
". The code then prints " true
" when it sees that $stmt
it is not null, which is because it is an object.
I recommend that you check the return value with $stmt->execute()
. You may have an SQL error. For example, if you misspelled the name of a table, or if the table does not exist in the database " dbname
" you are connected to, or the user you are logging on to does not have privileges to query that table.
Also check $stmt->errorInfo()
for more details on the error that occurred.
source to share