我正在尝试一些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);