PHP - Mysql_num_rows导致函数(OOP)

时间:2011-11-09 18:40:26

标签: php 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变量,以及如何调用?

2 个答案:

答案 0 :(得分:1)

你可以这样做,因为返回的数据是一个数组

$ db - >新woko();

$ pushComments = $ db-> pushComments();

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

希望有所帮助

答案 1 :(得分:0)

我相信你可以做count(var)来获得你的数组中的项目总数(看作第一个索引是唯一的ID。

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

即可
$num_rows = count($pushComments);