从php中的mysql表中选择count(*)

我能够获得mysql查询结果的值和行。

但我正在努力获得查询的单个输出。 例如。:

$result = mysql_query("SELECT COUNT(*) FROM Students;");

我需要显示结果。 但我没有得到结果。

我尝试过以下方法:

  1. mysql_fetch_assoc()
  2. mysql_fetch_row()
  3. mysql_fetch_row()

但我没有成功显示(获得)实际值。

Gana asked 2019-09-10T18:17:49Z
17个解决方案
181 votes

您需要使用as关键字对聚合进行别名,以便从mysql_fetch_assoc调用它

$result=mysql_query("SELECT count(*) as total from Students");
$data=mysql_fetch_assoc($result);
echo $data['total'];
Shakti Singh answered 2019-09-10T18:18:04Z
32 votes

如果您只需要值:

$result = mysql_query("SELECT count(*) from Students;");
echo mysql_result($result, 0);
bmaupin answered 2019-09-10T18:18:28Z
12 votes
$result = mysql_query("SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysql_fetch_assoc($result);
$count = $row['count'];

试试这个代码。

avetarman answered 2019-09-10T18:18:53Z
8 votes

请开始使用PDO。

mysql_ *自PHP 5.5.0起不推荐使用,将在7中完全删除。让我们更容易升级并立即开始使用它。

$dbh = new \PDO($dsn, $user, $password);
$sth = $dbh->prepare('SELECT count(*) as total from Students');
$sth->execute();
print_r($sth->fetchAll());
Lee Davis answered 2019-09-10T18:19:24Z
6 votes
$num_result = mysql_query("SELECT count(*) as total_count from Students ") or exit(mysql_error());
$row = mysql_fetch_object($num_result);
echo $row->total_count;
fatnjazzy answered 2019-09-10T18:19:41Z
1 votes
 $howmanyuser_query=$conn->query('SELECT COUNT(uno)  FROM userentry;');
 $howmanyuser=$howmanyuser_query->fetch_array(MYSQLI_NUM); 
 echo $howmanyuser[0];

经过这么多小时的优秀:)

Umut Sürmeli answered 2019-09-10T18:20:06Z
1 votes

我认为您的代码中存在拼写错误,您应该删除第二个到最后一个分号:

$result = mysql_query("SELECT COUNT(*) FROM Students;");
SureFoot answered 2019-09-10T18:20:31Z
0 votes

我认为这是更好的答案。

$query = "SELECT count(*) AS total FROM table_name"; 
mysql_select_db('database_name');
$result = mysql_query($query); 
$values = mysql_fetch_assoc($result); 
$num_rows = $values['total']; 
echo $num_rows;
Abdullah Al Shiam answered 2019-09-10T18:20:55Z
0 votes
$abc="SELECT count(*) as c FROM output WHERE question1=4";
$result=mysqli_query($conn,$abc);
if($result)
 {
    while($row=mysqli_fetch_assoc($result))
  {
        echo $row['c'];
  }     
 }

在此,它计算了question1列中出现的次数,它的工作完全正常

Pushkar Pokharkar answered 2019-09-10T18:21:19Z
0 votes

你也可以用它   并升级到mysqli_(停止使用mysql_*扩展...)

$result = mysqli_query($conn, "SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysqli_fetch_array($result);
$count = $row['count'];
echo'$count';
Legi Smith answered 2019-09-10T18:21:44Z
0 votes

使用mysql v5.7.20,以下是我能够使用PHP v7.0.22从表中获取行数的方法:

$query = "select count(*) from bigtable";
$qresult = mysqli_query($this->conn, $query);
$row = mysqli_fetch_assoc($qresult);
$count = $row["count(*)"];
echo $count;

第三行将返回如下所示的结构:

array(1) {
   ["count(*)"]=>string(4) "1570"
}

在这种情况下,结束echo语句将返回:

1570
AndyLovesRuby answered 2019-09-10T18:22:22Z
0 votes

对于mysqli用户,代码如下所示:

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

$result = $mysqli->query("SELECT COUNT(*) AS Students_count FROM Students")->fetch_array();
var_dump($result['Students_count']);

要么:

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

$result = $mysqli->query("SELECT COUNT(*) FROM Students")->fetch_array();
var_dump($result[0]);
Алексей Запруднов answered 2019-09-10T18:22:48Z
0 votes

使用count()函数和mysqli_fetch_array函数计算结果的完整行i表。 Count()函数用于计算表行,mysqli_fetch_arrayftech用于行计数值,之后您可以通过echo函数在页面上显示。

$sql="select count('1') from users";
$result=mysqli_query($mysqli,$sql);
$row=mysqli_fetch_array($result);
echo "<h3>$row[0]</h3>"; 

探索更多2个功能。

使用count()和mysqli_num_rows()计算表数据; 功能

Vishal Rana answered 2019-09-10T18:23:26Z
0 votes

您需要使用as关键字对聚合进行别名,以便从mysqli_fetch_assoc调用它

$result=mysqli_query($conn,"SELECT count(*) as total from Students");
$data=mysqli_fetch_assoc($result);
echo $data['total'];
Sani Kamal answered 2019-09-10T18:23:52Z
-1 votes
$qry_appr = "SELECT COUNT(*) FROM comments WHERE admin_panel_id ='$id' AND status = 'ON'";
$qry_data = mysqli_query($con, $qry_appr);
$approve_count = mysqli_fetch_array($qry_data);
$toatalCount = array_shift($approve_count);
echo $toatalCount;

这也可以,但这是通过移动fetch数组来返回0索引值。 欢迎大家

Samithe Adhikari answered 2019-09-10T18:24:17Z
-1 votes

这是用PHP显示表中没有行的代码

$sql="select count(*) as total from student_table";
$result=mysqli_query($con,$sql);
$data=mysqli_fetch_assoc($result);
echo $data['total'];
Raja Sekhar answered 2019-09-10T18:24:42Z
-4 votes

如果你只想要计数值,你可以做速记:

$cnt = mysql_num_rows(mysql_query('select * from students'));
Antiprosperous answered 2019-09-10T18:25:08Z
translate from https://stackoverflow.com:/questions/6907751/select-count-from-table-of-mysql-in-php