-3

Been struggling at this for a couple of hours now. Can anybody point out where I'm going wrong.

The first error I get is:

Fatal error: Call to undefined method PDO::rowCount() in
pm_wall.php on line 8
Call Stack: 0.0000 643680 1. {main}()
index.php:0 0.0015 662904 2.
include('pm_wall.php')
index.php:117

The second error I get is:

Warning: Invalid argument supplied for foreach() in pm_wall.php on line 19

Call Stack:
    0.0000     643296   1. {main}() index.php:0
    0.0015     649864   2. include(index.php:117

How do I go about resolving this? Here is my code below:

$sql = "SELECT * from pm_msg";
$result = $pdo->query($sql);
if($pdo->rowCount() > 0 && !empty($result)) // first error occurs here
{
    foreach ($result as $row) 
    {
        $user_id = $row['user_id'];
        echo '<div id="content">
                    <div class="stbody">
                    <div class="stimg">';

        // set profile picture  

        $sql = "SELECT img FROM pm_user WHERE id=$user_id";
        $result = $pdo->query($sql);
        foreach ($result as $row) // second error occurs here
        {
            echo $row['img'];
        }

        echo '<div id="stexpandbox"><div></div>';       
        echo '</div></div></div></div>';
    }
}
else
    {
        echo 'Wanna be starting soemthing?';
    }
4

3 に答える 3

3

rowCount() has to be called on your PDO statement, not on your PDO instance !

$result->rowCount(); Reading errors might help...

于 2013-02-02T13:54:45.450 に答える
2

Something like this maybe:

$sql = "SELECT * from pm_msg";
$result = $pdo->query($sql);
if($result->rowCount() > 0 && !empty($result)) // first error occurs here
{
    while( $row = $result->fetch(PDO::FETCH_ASSOC) ) 

Take a little time(a day or two), and read about PDO and its usage over on php.net.

于 2013-02-02T13:55:46.977 に答える
0

Use this option in PDO to get rows that are affected and matched:

PDO::MYSQL_ATTR_FOUND_ROWS   => TRUE

If the row is not found while update for instance - it will return 0. Hope this helps.

于 2014-08-07T10:39:59.107 に答える