php 从php中的mysql表中选择count(*)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6907751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select count(*) from table of mysql in php
提问by Gana
I am able to get both the value and row of the mysql query result.
我能够同时获得 mysql 查询结果的值和行。
But I am struggling to get the single output of a query. e.g.:
但我正在努力获得查询的单一输出。例如:
$result = mysql_query("SELECT COUNT(*) FROM Students;");
I need the result to display. But I am not getting the result.
我需要显示结果。但我没有得到结果。
I have tried with the following methods:
我尝试过以下方法:
mysql_fetch_assoc()
mysql_free_result()
mysql_fetch_row()
mysql_fetch_assoc()
mysql_free_result()
mysql_fetch_row()
But I didn't succeed to display (get) the actual value.
但是我没有成功显示(获取)实际值。
回答by Shakti Singh
You need to alias the aggregate using the as
keyword in order to call it from mysql_fetch_assoc
您需要使用as
关键字为聚合添加别名,以便从mysql_fetch_assoc
$result=mysql_query("SELECT count(*) as total from Students");
$data=mysql_fetch_assoc($result);
echo $data['total'];
回答by bmaupin
If you only need the value:
如果您只需要该值:
$result = mysql_query("SELECT count(*) from Students;");
echo mysql_result($result, 0);
回答by avetarman
$result = mysql_query("SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysql_fetch_assoc($result);
$count = $row['count'];
Try this code.
试试这个代码。
回答by Lee Davis
Please start using PDO.
请开始使用 PDO。
mysql_* is deprecated as of PHP 5.5.0 and will be removed entirely in 7. Let's make it easier to upgrade and start using it now.
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());
回答by fatnjazzy
$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;
回答by Raja Sekhar
here is the code for showing no of rows in the table with PHP
这是使用 PHP 显示表中没有行的代码
$sql="select count(*) as total from student_table";
$result=mysqli_query($con,$sql);
$data=mysqli_fetch_assoc($result);
echo $data['total'];
回答by Алексей Запруднов
For mysqli users, the code will look like this:
对于 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']);
or:
或者:
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
$result = $mysqli->query("SELECT COUNT(*) FROM Students")->fetch_array();
var_dump($result[0]);
回答by Sani Kamal
You need to alias the aggregate using the as
keyword in order to call it from mysqli_fetch_assoc
您需要使用as
关键字为聚合添加别名,以便从mysqli_fetch_assoc
$result=mysqli_query($conn,"SELECT count(*) as total from Students");
$data=mysqli_fetch_assoc($result);
echo $data['total'];
回答by Ram Pukar
回答by AndyLovesRuby
With mysql v5.7.20, here is how I was able to get the row count from a table using PHP v7.0.22:
使用 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;
The third line will return a structure that looks like this:
第三行将返回一个如下所示的结构:
array(1) {
["count(*)"]=>string(4) "1570"
}
In which case the ending echo statement will return:
在这种情况下,结束的 echo 语句将返回:
1570