103

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.

Lucio
  • 4,083
  • 3
  • 42
  • 72
Gana
  • 1,033
  • 2
  • 8
  • 4

12 Answers12

221

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'];
Kermit
  • 33,206
  • 11
  • 83
  • 119
Shakti Singh
  • 81,083
  • 20
  • 131
  • 150
32

If you only need the value:

$result = mysql_query("SELECT count(*) from Students;");
echo mysql_result($result, 0);
bmaupin
  • 12,667
  • 4
  • 75
  • 88
  • 2
    `echo mysql_result(mysql_query("SELECT count(*) from Students;"),0);` is saving one unnecessary variable – Max Muster Jan 08 '16 at 18:35
  • Correct; my answer was tailored to the question. – bmaupin Jan 08 '16 at 21:09
  • 4
    @eichertc the php interpreter has the variable always internal, as he needs to have the result in memory one way or the other. So your way just makes the code worser to read and maintain, IMHO. – Tom Mar 14 '17 at 13:49
16
$result = mysql_query("SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysql_fetch_assoc($result);
$count = $row['count'];

Try this code.

ban-geoengineering
  • 17,070
  • 21
  • 157
  • 242
avetarman
  • 1,234
  • 9
  • 8
13

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.

$dbh = new \PDO($dsn, $user, $password);
$sth = $dbh->prepare('SELECT count(*) as total from Students');
$sth->execute();
print_r($sth->fetchColumn());
Dharman
  • 26,923
  • 21
  • 73
  • 125
Lee Davis
  • 4,507
  • 2
  • 26
  • 39
9

here is the code for showing no of rows in the table with PHP

$sql="select count(*) as total from student_table";
$result=mysqli_query($con,$sql);
$data=mysqli_fetch_assoc($result);
echo $data['total'];
Sani Kamal
  • 1,061
  • 14
  • 25
Raja Sekhar
  • 101
  • 1
  • 2
7
$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
  • 5,840
  • 11
  • 54
  • 82
3

You can as well use this and upgrade to mysqli_ (stop using mysql_* extension...)

$result = mysqli_query($conn, "SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysqli_fetch_array($result);
$count = $row['count'];
echo $count;
Dharman
  • 26,923
  • 21
  • 73
  • 125
MK Smith
  • 101
  • 9
3

With mysql v5.7.20, here is how I was able to get the row count from a table using 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:

1570
AndyLovesRuby
  • 123
  • 1
  • 3
3

For mysqli users, the code will look like this:

$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]);
1

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

$result=mysqli_query($conn,"SELECT count(*) as total from Students");
$data=mysqli_fetch_assoc($result);
echo $data['total'];
Sani Kamal
  • 1,061
  • 14
  • 25
1
$db  = new PDO('mysql:host=localhost;dbname=java_db', 'root', 'pass');
$Sql = "SELECT count(*) as `total` FROM users";
$stmt = $db->query($Sql);
$stmt->execute();
$total = $stmt->fetch(PDO::FETCH_ASSOC);
print '<pre>';
print_r($total);
print '</pre>';

Result:

enter image description here

Dharman
  • 26,923
  • 21
  • 73
  • 125
Ram Pukar
  • 1,377
  • 11
  • 15
0
 $howmanyuser_query=$conn->query('SELECT COUNT(uno)  FROM userentry;');
 $howmanyuser=$howmanyuser_query->fetch_array(MYSQLI_NUM); 
 echo $howmanyuser[0];

after the so many hours excellent :)