[英]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.