我想问一下如何在php中回显sql的计数?
$sql = $conn->query("SELECT count(*) as total FROM visitor");
$row = mysql_fetch_array($sql);
Echo $row['total'];
答案 0 :(得分:3)
首先不要将mysql _ 和mysqli扩展混合在一起,你不能通过这样做得到任何东西。
其次,非常重要的是,在PHP 7中不推荐使用mysql _ ,你可以使用mysqli _ 或PDO。
以下是MYSQLi Objected Oriented的基本示例:
<?php
// Create connection
$conn = new mysqli($YourServerName, $Yourusername, $Yourpassword, $Yourdbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT COUNT(*) AS Total FROM Table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_row();
echo 'Total:'. $row[0]; // print total record
}
else
{
// if record not exist.
}
?>
答案 1 :(得分:0)
$row = $conn->query($sql);
$data = $row->fetch();
echo $data['total'];