tgcowell
tgcowell

Reputation: 95

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:

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();
$sql = ("SELECT COUNT(*) FROM project_directory");
$result = mysql_fetch_array($sql);
?>

<?php echo $result ; ?> 

The results are 28 and work if i run the following within the SQL box from phpMyAdmin

SELECT COUNT(*) FROM project_directory

Appreciate anyone help or advice.

Upvotes: 6

Views: 46326

Answers (7)

Samithe Adhikari
Samithe Adhikari

Reputation: 131

$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

Upvotes: 1

Pushkar Pokharkar
Pushkar Pokharkar

Reputation: 11

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

ITS works completely

Upvotes: 1

Bhoomi
Bhoomi

Reputation: 1

You have two options.

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.

Upvotes: 0

Agha Umair Ahmed
Agha Umair Ahmed

Reputation: 1020

if you are new in php and mysql try to use mysqli not mysql

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

$sql = "SELECT COUNT(*) FROM project_directory";
$query = mysql_query($sql);
$result = mysql_num_rows($query);
echo $result ;
?>

Upvotes: 0

Soosh
Soosh

Reputation: 812

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

Upvotes: 2

Sandy Marwal
Sandy Marwal

Reputation: 19

Hello , if you are fresher then you can read w3schools.com enjoy

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

$sql = "SELECT COUNT(*) as count FROM project_directory";
$result = mysql_num_rows($sql);
$my=$result['count'];
echo $my;
?>

Upvotes: 1

M I
M I

Reputation: 3682

you did not execute the query using mysql_query() function.

you need to do like this

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();

$sql = ("SELECT COUNT(*) FROM project_directory");
$rs = mysql_query($sql);
 //-----------^  need to run query here

 $result = mysql_fetch_array($rs);
 //here you can echo the result of query
 echo $result[0];

?>

<?php echo $result[0]; ?> 

Note: if you have started learning PHP/Mysql then try to use mysqli_* functions. mysql_ will be deprecated in future PHP versions.

Upvotes: 8

Related Questions