Jump to content

PHP pdo mysql returns zero result, why?


colap

Recommended Posts

            $dbh = mysql_connection();
            $sql = "select * from posts";
            $stmt = $dbh->prepare($sql);
            $rowCount = $stmt->rowCount();
            $result = $stmt->fetchAll();
            formatted_value($result);
            $stmt->execute();
        ?>

It returns empty array, What can be the reason?

Link to comment
https://forums.phpfreaks.com/topic/296842-php-pdo-mysql-returns-zero-result-why/
Share on other sites

until you execute() the query, there's nothing for rowCount() or fetchAll() to use and are likely throwing php errors. also, why are you using a prepared query when the sql statement doesn't contain any input values?

 

did you actually write this code or copy it from somewhere? i ask that because i reviewed two of your older threads that also use PDO statements and you previously got the prepare/execute/rowcount statements in the correct order for a SELECT query. programming requires that you actually learn what each statement does so that you can put them together in a meaningful way each time you use them.

How do you expect to get any rows before you've execute()d the query?

It is working now.

        <?php
            $dbh = mysql_connection();
            $sql = "select * from posts";
            $stmt = $dbh->prepare($sql);
            $stmt->execute();
            $result = $stmt->fetchAll();
            formatted_value($result);
        ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.