SQL行数 [英] SQL Number of rows
问题描述
我试图拉取SQL表中的总行数。
I am trying to pull the total number of rows in a SQL table.
我使用以下代码:
$rowNum = mysql_query("SELECT COUNT(*) FROM Logs");
$count = mysql_fetch_assoc($rowNum);
echo "Rows: " . $count;
但是,我得到的输出是 Rows:Array
而不是像 Rows:10
。
However, the output I get is Rows: Array
rather than something like Rows: 10
.
任何想法我做错了什么?
Any idea what I'm doing wrong?
推荐答案
mysql_fetch_assoc()
返回一个关联数组,结果列名称为键,结果值为值。如果你运行 var_dump($ rowNum)
,你会看到一个数组 COUNT(*)
数字作为值。您可以使用 $ rowNum [COUNT(*)]
,或者更好的是,别名count表达式,并使用别名来引用该值。
mysql_fetch_assoc()
returns an associative array with the result column names as keys and the result values as values. If you run var_dump($rowNum)
, you'll see an array with COUNT(*)
as key and the number as value. You can use $rowNum["COUNT(*)"]
or, better, alias the count expression and use the alias to refer to the value.
$rowNum = mysql_query("SELECT COUNT(*) AS total FROM Logs");
$count = mysql_fetch_assoc($rowNum);
echo "Rows: " . $count["total"];
这篇关于SQL行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!