CodeGuy
CodeGuy

Reputation: 28905

MySQL - count total number of rows in php

What is the best MySQL command to count the total number of rows in a table without any conditions applied to it? I'm doing this through php, so maybe there is a php function which does this for me? I don't know. Here is an example of my php:

<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("some command");
$row = mysql_fetch_array($result);

mysql_close($con);
?>

Upvotes: 29

Views: 357596

Answers (12)

ucMax
ucMax

Reputation: 5496

It`s better to count rows directly in the database, it is more efficient for large tables.

$tableName = 'my-table'
$sql = "SELECT COUNT(*) FROM $tableName";
$row = mysqli_fetch_assoc(mysqli_query($conn, $sql));
$row_count = $row['COUNT(*)'];

Upvotes: 0

Subhan Raj
Subhan Raj

Reputation: 9

Well, I used the following approach to do the same: I have to get a count of many tables for listing the number of services, projects, etc on the dashboard. I hope it helps.

PHP Code

// create a function 'cnt' which accepts '$tableName' as the parameter.

 function cnt($tableName){
        global $conection;
        $itemCount = mysqli_num_rows(mysqli_query($conection, "SELECT * FROM `$tableName`"));
        echo'<h6>'.$itemCount.'</h6>';
    }

Then when I need to get the count of items in the table, I call the function like following

<?php
  cnt($tableName = 'projects');
?>

In my HTML front end, so it renders the count number

  • It's to be noted that I create the cnt() function as a global function in a separate file which I include in my head, so I can call it from anywhere in my code.

Upvotes: -1

Shridhar
Shridhar

Reputation: 339

you can do it only in one line as below:

$cnt = mysqli_num_rows(mysql_query("SELECT COUNT(1) FROM TABLE"));
echo $cnt;

Upvotes: 6

chobela
chobela

Reputation: 305

mysqli_num_rows is used in php 5 and above.

e.g

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);
  printf("Result set has %d rows.\n",$rowcount);
  // Free result set
  mysqli_free_result($result);
  }
mysqli_close($con);
?>

Upvotes: 9

Toki
Toki

Reputation: 294

use num_rows to get correct count for queries with conditions

$result = $connect->query("select * from table where id='$iid'");
$count=$result->num_rows;
echo "$count";

Upvotes: 4

Amirhosein jaafari
Amirhosein jaafari

Reputation: 21

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);
  echo "number of rows: ",$rowcount;
  // Free result set
  mysqli_free_result($result);
  }

mysqli_close($con);
?>

it is best way (I think) to get the number of special row in mysql with php.

Upvotes: 2

Divyasundar Sahu
Divyasundar Sahu

Reputation: 19

<?php
$conn=mysqli_connect("127.0.0.1:3306","root","","admin");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select count('user_id') from login_user";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
echo "$row[0]";
mysqli_close($conn);
?>

Still having problem visit my tutorial http://www.studentstutorial.com/php/php-count-rows.php

Upvotes: 1

Asesha George
Asesha George

Reputation: 2268

for PHP 5.3 using PDO

<?php
    $staff=$dbh->prepare("SELECT count(*) FROM staff_login");
    $staff->execute();
    $staffrow = $staff->fetch(PDO::FETCH_NUM);
    $staffcount = $staffrow[0];


    echo $staffcount;
?>

Upvotes: 2

fdaines
fdaines

Reputation: 1246

<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("select count(1) FROM table");
$row = mysql_fetch_array($result);

$total = $row[0];
echo "Total rows: " . $total;

mysql_close($con);
?>

Upvotes: 57

Jules
Jules

Reputation: 7233

Either use COUNT in your MySQL query or do a SELECT * FROM table and do:

$result = mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($result);
echo "There are " . $rows . " rows in my table.";

Upvotes: 27

uadnal
uadnal

Reputation: 11445

$sql = "select count(column_name) as count from table";

Upvotes: 0

Jon Gauthier
Jon Gauthier

Reputation: 25592

Use COUNT in a SELECT query.

$result = mysql_query('SELECT COUNT(1) FROM table');
$num_rows = mysql_result($result, 0, 0);

Upvotes: 6

Related Questions