Home > Bookmarks, MySQL, Networking, PhP, Tech > Mysql Query – Get Number of Rows in a Mysql Database Table

Mysql Query – Get Number of Rows in a Mysql Database Table

1’st Solution, use Mysql COUNT(*) :

$sql = "SELECT COUNT(*) FROM my_table";
$result = mysql_query($sql);
$num = mysql_result($result, 0);
echo $num;

2′nd solution: use TABLE_ROWS and TABLE_NAME from information_schema:

$query = "select TABLE_ROWS from information_schema.tables where table_schema = 'my_data_base' AND TABLE_NAME = 'my_table' ";
$result = mysql_query($query);
$count =  mysql_fetch_row($result);
echo $count[0];

3′rd solution – get count rows foar all tables – use TABLE_ROWS and TABLE_NAME from information_schema:

$query = "select TABLE_NAME,TABLE_ROWS from information_schema.tables where table_schema = 'my_data_base' ";
$result = mysql_query($query);
while ($row =  mysql_fetch_row($result)) {
$rowcount[$row[0]] = $row[1];
}
print_r($rowcount);

Get number of rows for custom tables with: $rowcount['my_table'];

  1. No comments yet.
  1. No trackbacks yet.
GoCache - ByREV-Cache v1.0 - live served in : 0.182419 sec (gzip)