select count(*) from table of mysql in php

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.

Answers


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'];

If you only need the value:

$result = mysql_query("SELECT count(*) from Students;");
echo mysql_result($result, 0);

$result = mysql_query("SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysql_fetch_assoc($result);
$count = $row['count'];

Try this code.


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->fetchAll());

$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;

 $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 :)


$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'];
  }     
 }

In this its count the number of occurrences in the question1 column, Its work completely


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';

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

I think there is a typo in your code and you should remove the second to last semi-colon in:

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

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

Count complete row i table using count() function and mysqli_fetch_array function for the result . Count() function is used to count the table rows and mysqli_fetch_arrayftech the rows count values after that you can display on the page via echo function.

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

Explore for more 2 functions.

Count table data using count() and mysqli_num_rows(); function


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'];

I think it's better answer.

$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;

$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;

This will also fine but this is do what returning 0 index value by shifting fetch array. welcome all


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'];

If you only want the count value you could do shorthand:

$cnt = mysql_num_rows(mysql_query('select * from students'));

Need Your Help

UI Router conditional ui views?

angularjs angular-ui angular-ui-router

I can't figure out a reasonable way, which doesn't feel like a hack, to solve this rather trivial problem.

Create a .eml (email) file in Java

java email outlook eml

Anybody knows how to do this? I got all the information of the email (body, subject, from , to, cc, bcc) and need to generate an .eml file out of it.