Home Sql Mysql-example PHP SQL Number of Rows



PHP SQL Number of Rows
Posted on: January 20, 2009 at 12:00 AM
To understand how to count the number of rows in a table, we have created a sql_num_rows.php file.

PHP SQL Number of Rows

     

This example illustrates how to count the rows of the table.

To understand how to count the number of rows in a table, we have created a sql_num_rows.php file. If you are not connected to the database, then create a connection first using mysql_connect("hostName", "userName", "Password") and mysql_select_db("databaseName", connectionObject). Now the query can be executed using mysql_query("SELECT * FROM tableName"). The number of rows are returned by the mysql_num_rows() method.

 

 

 

Table: users

 

Source Code of sql_num_rows.php 

<?php
  $con = mysql_connect('localhost','root','root');
  if (!$con) {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("test", $con);

  $sql = "SELECT * from users";

  $res = mysql_query($sql);

  if (!$res) {
  print("SQL statement failed with error:\n");
  print(mysql_error($con));
  else {
  $number_of_rows = mysql_num_rows($res);
  print("$number_of_rows rows found");
  }  
  mysql_close($con);
?>
Download Source Code

 

Output:

Related Tags for PHP SQL Number of Rows:
phpsqlmysqlcdatabasefiletabledataselectobjectioconnectioncountuserpasswordwordnumbernameusingcreatetabrowconnectifrowsctetohostusernamebasewseilhostnamenotusefirstinnopassasstamntososjdbasemehphowobjbasenamescreatedatmyirhaeaandarconnectedssrdthswavstababljendono


More Tutorials from this section

Ask Questions?    Discuss: PHP SQL Number of Rows  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.