Display the results of a SELECT COUNT(*) in PHP
Current trying to display the results of a SELECT COUNT(*) from SQL within my website. I’m 100% new to PHP and SQL so understand this must be the basics! If anyone could recommend a good book or website to learn that would also be great. Here is my current code:
SELECT COUNT(*) FROM project_directory
7 Answers 7
you did not execute the query using mysql_query() function.
Note: if you have started learning PHP/Mysql then try to use mysqli_* functions. mysql_ will be deprecated in future PHP versions.
Hi Maz, I’m getting the following error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in
please first check if you db connection is ok. mysql_connect is ok then mysql_select_db is correct. 3rd. print your query and run it directly in phpmyadmin and see there is not issue in query
$result is an array, so you should use $result[0] or $result[0][0]
if you use print_r($result) you will see the structure of your array and which once you should use.
also you did not use mysql_query($sql) .
you should use it like:
$result = mysql_query($sql); $out = mysql_fetch_array($result); print($out[0][0]); // or print($out[0]);
$result = mysql_fetch_array($sql); will cause the error because mysql_fetch_array() method needs resource argument, but the OP use $sql which is a string.
This was the only one that worked for me.. geeez.. how hard can it be..HARD apparently $sql = «SELECT COUNT(*) FROM genericsignup WHERE REF LIKE ‘».$compref.»‘»; $query = mysqli_query($db_conn,$sql); $out = mysqli_fetch_array($query); print($out[0]);
Hello , if you are fresher then you can read w3schools.com enjoy
again! $result = mysql_num_rows($sql); will cause the error because mysql_num_rows() method needs resource argument but $sql is just a string. incorrect answer!
I’m just confused, nothing has worked. I continue to get a number of errors. I assumed this would be easy? It’s seems to be a lot harder than expected.
it is very easy, @tgcowell, please check Amir Noori and Agha Umair Ahmed‘s answers again. their answers are corrected
$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']; >>
$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.this can use without alias. welcome all
if you are new in php and mysql try to use mysqli not mysql
$result = mysql_num_rows($sql); will cause the error . because the method mysql_num_rows() needs resource argument but $sql is simply a string.
you need to execute query first $query = mysql_query($sql); and then $result = mysql_num_rows($query); sorry not written execute query
Get the result from the resource returned by the count query:
$resource = mysql_query("SELECT COUNT(col) FROM table"); $count = mysql_result($resource,0);
Or, get the number of rows from the resource returned by the query (without count).
$resource = mysql_query("SELECT col FROM table WHERE col IS NOT NULL"); $count = mysql_num_rows($resource);
I would recommend that you use the first, the reason being is that it is unnecessary to extract all the data from the table when you only need the count.
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;");
But I didn’t succeed to display (get) the actual value.
12 Answers 12
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);
echo mysql_result(mysql_query(«SELECT count(*) from Students;»),0); is saving one unnecessary variable
@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.
$result = mysql_query("SELECT COUNT(*) AS `count` FROM `Students`"); $row = mysql_fetch_assoc($result); $count = $row['count'];
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());
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'];
$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;
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:
In which case the ending echo statement will return: