簡體   English   中英

如何打印mysql_num_rows?

[英]how to print mysql_num_rows?

我有這樣的數據庫結構 在此處輸入圖片說明

並試圖打印行數,但只得到1

正在使用的代碼是

$sql="select Count(*) from uniqueviews where hour=13"; $result=mysql_query($sql); $ml=mysql_num_rows($result); echo $ml;

根據查詢,它應該打印6但其打印1

哪里做錯了?

我認為它算行嗎?

行數計數后如何打印結果?

Count(*)僅返回一行,其中包含行數。 這就是為什么您只獲得1作為返回值的原因。 如果需要實際值,可以執行以下操作:

$sql="select * from uniqueviews where hour=13"; 
$result=mysql_query($sql); 
$ml=mysql_num_rows($result); 
echo $ml;

或這個:

$sql="select COUNT(*) from uniqueviews where hour=13"; 
$result=mysql_query($sql);  
$row = mysql_fetch_array($result, MYSQL_NUM);
$ml=$row[0];
echo $ml;

實際上,您正在打印查詢返回的行數。由於要計算結果,因此應使用mysql_fetch_array來獲取查詢結果。

 $result = select Count(*) from uniqueviews where hour=13 // $result is 6 here

 and when you use mysql_count_rows($result) it returns 1 which is correct

 try printing $result which should be 6

或者您可以在查詢中用*替換count(*) ,它應該為您提供正確的結果

你錯過了什么。

如果執行my_sql_num_rows($result) ,它將僅顯示返回的總行 ,按照這種邏輯,只有一行的結果是完全正確的( count(*)僅返回一行)。

如果您想保持這種邏輯,請不要在SQL查詢中使用count

SELECT * from uniqueviews where hour = 13

或者,如果您想保留該sql查詢,請更改獲取結果的方式:

mysql_fetch_array($result, MYSQL_NUM);

嘗試這個:

$sql    = "select Count(*) AS COUNT from uniqueviews where hour=13";
$result = mysql_query($sql); 
$count  = mysql_fetch_object($result);

echo $count->COUNT;
"select Count(*) from uniqueviews where hour=13"

這只會返回計數,即僅返回一行。

如果您想查看查詢結果,即總數

然后做這樣的事情

$sql="select Count(*) from uniqueviews where hour=13"; 
$result=mysql_query($sql);
$ml=mysql_fetch_array($result); 
echo $ml[0];

您的查詢返回的行數始終為1,因為僅返回1條記錄-記錄中包含該選擇的行數。

更換:

$sql="select Count(*) from uniqueviews where hour=13";

附:

$sql="select id from uniqueviews where hour=13";

然后使用mysql_num_rows。

但是您應該獲得由count()返回的值,mysql_num_rows不適用於此類操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM