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'];
Categories: Bookmarks, MySQL, Networking, PhP, Tech get count row, information_schema, Mysql COUNT, mysql row counts, SELECT COUNT, TABLE_NAME, TABLE_ROWS, table_schema








