从php中的mysql表中选择count(*)
我能够获取 mysql 查询结果的值和行。
但我正在努力获取查询的单个输出。例如:
$result = mysql_query("SELECT COUNT(*) FROM Students;");
我需要显示结果。但我没有得到结果。
我尝试过以下方法:
mysql_fetch_assoc()
mysql_free_result()
mysql_fetch_row()
但我没有成功显示(获取)实际值。
I am able to get both the value and row of the mysql query result.
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()
But I didn't succeed to display (get) the actual value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您需要使用
as
关键字为聚合别名,以便从mysql_fetch_assoc
调用它You need to alias the aggregate using the
as
keyword in order to call it frommysql_fetch_assoc
如果您只需要该值:
If you only need the value:
试试这个代码。
Try this code.
请开始使用 PDO。
mysql_* 从 PHP 5.5.0 开始已弃用,并将在 7 中完全删除。让我们更轻松地升级并立即开始使用它。
Please start using 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.
这是使用 PHP 显示表中行数的代码
here is the code for showing no of rows in the table with PHP
您也可以使用它并升级到
mysqli_
(停止使用mysql_*
扩展...)You can as well use this and upgrade to
mysqli_
(stop usingmysql_*
extension...)使用 mysql v5.7.20,以下是我如何使用 PHP v7.0.22 从表中获取行计数:
第三行将返回如下所示的结构:
在这种情况下,结束 echo 语句将返回:
With mysql v5.7.20, here is how I was able to get the row count from a table using PHP v7.0.22:
The third line will return a structure that looks like this:
In which case the ending echo statement will return:
对于 mysqli 用户,代码将如下所示:
或:
For mysqli users, the code will look like this:
or:
结果:
Result:
您需要使用
as
关键字为聚合别名,以便从mysqli_fetch_assoc
调用它You need to alias the aggregate using the
as
keyword in order to call it frommysqli_fetch_assoc
经过这么多个小时的努力,非常棒:)
after the so many hours excellent :)