Jump to content

PHP pdo mysql returns zero result, why?


colap
Go to solution Solved by requinix,

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
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.

Edited by mac_gyver
Link to comment
Share on other sites

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);
        ?>

Edited by php-coder
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.