简体   繁体   中英

PHP - Mysql_num_rows on result in function (OOP)

I'm trying with some OOP and got a problem with making a mysql_num_rows on a query in a function. I got a function looking like this:

    function userPush()
{
    $sql = ("SELECT *
                FROM pushComments
                WHERE pushCommentsFromProfileId='$_SESSION[userId]'
            ");
    $result = mysql_query($sql)or die(mysql_error());

    $userPush = Array();

    while($row = mysql_fetch_assoc($result)):

        $userPush[$row['pushCommentsId']]['pushCommentsId'] = $row['pushCommentsId'];
        $userPush[$row['pushCommentsId']]['pushCommentsTimestamp'] = $row['pushCommentsTimestamp'];
        $userPush[$row['pushCommentsId']]['pushCommentsFromProfile'] = $row['pushCommentsFromProfile'];
        $userPush[$row['pushCommentsId']]['pushCommentsFromProfileId'] = $row['pushCommentsFromProfileId'];
        $userPush[$row['pushCommentsId']]['pushCommentsContent'] = $row['pushCommentsContent'];

    endwhile;

    return $userPush;
}
//

And calling the function like this:

$db -> New woko();
$pushComments = $db->pushComments();

How would I create a mysql_num_rows here? Can I call a $count variable from the function, and how?

You can do it like this since the data that is being returned is an array

$db -> New woko();

$pushComments = $db->pushComments();

$pushCommentsCount = count($db->pushComments());

hope that helps

I believe you can just do count(var) to get the total number of items on your array (seen as the first index is the unique ID.

just add this line to the bottom of your script

$num_rows = count($pushComments);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM