简体   繁体   English

PHP-函数结果(OOP)上的Mysql_num_rows

[英]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. 我正在尝试一些OOP,但在函数中的查询上制作mysql_num_rows时遇到问题。 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? 我如何在这里创建mysql_num_rows? Can I call a $count variable from the function, and how? 我可以从函数中调用$ count变量吗?

You can do it like this since the data that is being returned is an array 您可以这样做,因为返回的数据是数组

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

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

$pushCommentsCount = count($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. 我相信您可以执行count(var)来获取数组中项目的总数(因为第一个索引是唯一ID)。

just add this line to the bottom of your script 只需将此行添加到脚本的底部

$num_rows = count($pushComments);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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