The PHP MySQL library functions covered in the previous tutorials are probably enough to build a simple web application.
Below are some additional functions that are frequently used in web applications.
The mysql_num_rows() Function
This function can be used after a SELECT query to find the number of rows that the query returned.
It takes only one argument - the result set returned from the previous SELECT query and returns the number of rows.
Example of a Row Count after a Select Query
<?php
$query = "SELECT * FROM people";
$result = mysql_query($query, $connection) or exit(mysql_error());
$num_rows = mysql_num_rows($result);
echo($num_rows);
?>
Note: If you only require the number of rows in a table and not the contents then the code above is inefficient. Instead you should use SELECT COUNT(*) as shown below.
This function can be used after a SELECT query to find the number of attributes in a row that the query returned.
It takes only one argument - the result set returned from the previous SELECT query and returns the number of attributes.
Example of an Attribute Count after a Select Query
<?php
$query = "SELECT * FROM people";
$result = mysql_query($query, $connection) or exit(mysql_error());
$num_fields = mysql_num_fields($result);
echo($num_fields);
?>
The mysql_data_seek() Function
This function is used to return a result that starts at a row other than the first row. The function is run after the SELECT query but before fetching a row.
It requires two arguments, the result set from SELECT query and the offset from the first row.