How to display count value 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.

How to display count value in php

Kiran RS

95214 silver badges31 bronze badges

asked Jan 23, 2014 at 6:47

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.

answered Jan 23, 2014 at 7:01

Maz IMaz I

3,6262 gold badges23 silver badges38 bronze badges

2

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

answered Jan 23, 2014 at 6:53

How to display count value in php

SooshSoosh

7921 gold badge7 silver badges23 bronze badges

4

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

answered Jan 23, 2014 at 7:08

How to display count value in php

3

$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

answered Sep 14, 2017 at 9:36

How to display count value in php

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

answered Jan 23, 2014 at 7:00

2

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.

answered Jan 23, 2014 at 6:55

3

$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

HDJEMAI

9,16945 gold badges68 silver badges89 bronze badges

answered Jun 29, 2017 at 2:30

How to display count value in php

What does count () do in PHP?

The count() function returns the number of elements in an array.

How do you count elements in an array?

A number of elements present in the array can be found by calculating the length of the array..
STEP 1: START..
STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}.
STEP 3: length= sizeof(arr)/sizeof(arr[0]).
STEP 4: PRINT "Number of elements present in given array:" by assigning length..
STEP 5: RETURN 0..
STEP 6: END..

How do I count the number of columns in PHP?

Use PDOStatement::columnCount() to return the number of columns in the result set represented by the PDOStatement object. If the PDOStatement object was returned from PDO::query(), the column count is immediately available.

What is difference between size and count PHP?

1. The sizeof() function is used to return the number of elements in an array. The count() returns the number of elements in the array.

Tải thêm tài liệu liên quan đến bài viết How to display count value in php