0

我无法用我当前的配置来计算数据库中的用户数。

<?php
require_once 'scripts/app_config.php';
mysqli_connect(DATABASE_HOST,DATABASE_USER,DATABASE_PASS,DATABASE_NAME);
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo "There are: ". $num . " users in the database";
mysqli_close($con);
?>
4

2 回答 2

1

你的问题是这样的:

$num = mysql_num_rows($result);

您的查询仅返回 1 行,因此我认为您遇到的问题是它始终输出:There are 1 users in the database

您应该改用:

$num = mysqli_fetch_array($query)[0];
于 2013-11-04T05:44:32.307 回答
1

要么使用要么COUNT(*)使用mysql_num_rows

$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$num = $row[0];

echo "There are: ". $num . " users in the database";


注意: mysql_*功能已弃用,请mysqli_*尽快使用功能。

于 2013-11-04T05:46:50.333 回答