从php中的mysql表中选择count(*) [英] select count(*) from table of mysql in php

查看:156
本文介绍了从php中的mysql表中选择count(*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以得到mysql查询结果的值和行。



但我很难得到查询的单个输出。例如:

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

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



我尝试了以下方法:


  1. mysql_fetch_assoc()

  2. mysql_free_result
  3. mysql_fetch_row()

您需要使用别名来对聚合进行别名,作为关键字,以便从 mysql_fetch_assoc

中调用

  $ result = mysql_query(SELECT count(*)as total from Students); 
$ data = mysql_fetch_assoc($ result);
echo $ data ['total'];


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:

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

But I didn't succeed to display (get) the actual value.

解决方案

You need to alias the aggregate using the as keyword in order to call it from mysql_fetch_assoc

$result=mysql_query("SELECT count(*) as total from Students");
$data=mysql_fetch_assoc($result);
echo $data['total'];

这篇关于从php中的mysql表中选择count(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆