3

I'm executing a simple query, without bound parameters, using PDO. I have tested it directly against my database and it executes cleanly, returning the expected results. However, when I plug it in to my PDO object and call fetchAll(), it returns an empty array.

$query = 'SELECT count(*) as mycount FROM mytable';
$mysql = $connection->prepare($query);
$result = $mysql->fetchAll();

print_r($result);

Expected result:

array
(
    [mycount] => 8
)

Actual result:

array
(
)

Any ideas what might be causing this, or how to go about troubleshooting this?

0

2 Answers 2

13

You've prepared, but haven't executed the statement. You need

$mysql->execute();

first

Sign up to request clarification or add additional context in comments.

1 Comment

Adding: If you don't need to bind parameters, use PDO::query instead of preparing a statement...
4

First you need to:

$mysql->execute();

Then you can

$result = $mysql->fetchAll();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.