Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - count total number of rows in php

Tags:

php

mysql

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); ?> 
like image 565
CodeGuy Avatar asked Jul 11 '11 19:07

CodeGuy


People also ask

How do I COUNT number of rows in MySQL?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

How can I see the number of rows in PHP?

The mysqli_num_rows() function is an inbuilt function in PHP which is used to return the number of rows present in the result set. It is generally used to check if data is present in the database or not. To use this function, it is mandatory to first set up the connection with the MySQL database.

How can we find the number of rows in a result set using PHP?

The mysqli_num_rows() function returns the number of rows in a result set.

How do I COUNT rows in SQL?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).


1 Answers

<?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); ?> 
like image 196
fdaines Avatar answered Sep 24 '22 11:09

fdaines