我对PHP不太好,但我知道其他一些类似的语言。
现在我要做的是获取表中有多少行。这是我的代码。
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
现在这个代码返回1,当实际有3.这是为什么? 谢谢你的帮助。
答案 0 :(得分:0)
试试这样:
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = mysql_fetch_object($result);
print_r($num_rows);
注意:不推荐使用Mysql_ *函数。因此不推荐。
答案 1 :(得分:0)
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT COUNT(*) as total_row FROM list");
echo "<table border='1'>
<tr>
<th>total_row</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['total_row'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
答案 2 :(得分:0)
$result = mysql_query("SELECT COUNT(*) as total FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows; // output 1;
$rows = mysql_fetch_object($result);
echo $rows->total; // output 3;
答案 3 :(得分:0)
你应该这样做:
$result = mysql_query("SELECT * FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
如果您希望获得COUNT
的总行数,可以这样做:
$result = mysql_query("select count(*) FROM list");
$row = mysql_fetch_array($result);
echo $row[0];
答案 4 :(得分:0)
检查此
$result = mysql_fetch_array(mysql_query("SELECT COUNT(*) as count FROM list"));
echo $result['count'];
答案 5 :(得分:0)
它返回单列中的行数,然后只返回1
<强>参见强>: http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
答案 6 :(得分:0)
仅更改您的查询
$result = mysql_query("SELECT * FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
答案 7 :(得分:0)
它返回一行,因为下面的查询只返回一个值,即行数。
$result = mysql_query("SELECT COUNT(*) FROM list");
我认为这应该有所帮助:
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = $result->fetchColumn();
echo $num_rows;