繁体   English   中英

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

[英]PHP - Mysql_num_rows on result in function (OOP)

我正在尝试一些OOP,但在函数中的查询上制作mysql_num_rows时遇到问题。 我有一个看起来像这样的函数:

    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;
}
//

并像这样调用函数:

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

我如何在这里创建mysql_num_rows? 我可以从函数中调用$ count变量吗?

您可以这样做,因为返回的数据是数组

$ db-> New woko();

$ pushComments = $ db-> pushComments();

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

希望能有所帮助

我相信您可以执行count(var)来获取数组中项目的总数(因为第一个索引是唯一ID)。

只需将此行添加到脚本的底部

$num_rows = count($pushComments);

暂无
暂无

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

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