11 Vote

MySQL: How many records have been found or are affected by my query?

Info by Computer Expert | 30.05.2012 at 19:19

Sometimes, we want to know in PHP, how many records were found in a MySQL search query with SELECT in total or how many rows were affected by an UPDATE. Both parameters can be easily read out by a PHP-MySQL function. Here, I will show you how to do it:

How many records have been found?

We have an arbitrary SELECT query and we are interested in how many records have been found without going through all lines of the result:

$query = "SELECT name FROM tab WHERE zipcode=46242";
$result = mysql_db_query($database, $query, $connection);
 
echo mysql_num_rows()," people have been found.";

The function mysql_num_rows() gives us the number of records that were found in the last query. This information is interesting, for example, to display the number of hits for a search or to decide whether a further page of search results is needed.

How many records are affected?

After an UPDATE we are interested in how many records were actually affected and changed by the UPDATE:

$query = "UPDATE tab SET town='Bottrop' WHERE zipcode=46242";
$result = mysql_db_query($database, $query, $connection);
 
echo mysql_affected_rows()," records have been changed.";

The function mysql_affected_rows() gives us the number of modified records that were affected in the last MySQL query. This information is interesting, for example, to check whether any rows have been changed after an UPDATE.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

MySQL: Whole Word Search
Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.