PHP: Check If MySql Query is Successful

Checking of a mysql query for success or not is very easy by using PHP. Simply you can count the resulting rows extracted by a mysql query. We will use PHP mysql_num_rows function to get number rows in the result of a query execution. Below is the method, if mysql query is successful or not:

$thequery = mysql_query(“SELECT * from `your_table`, $con);
//Apply condition to filter out whether the query returns results or not
//Query will result ‘0’ if nothing found, else it will return results 1 to rows_numbers

But after the release of PHP 5.5.0, mysql_num_rows() function is deprecated and will be removed in the future versions of PHP. So I will recommend not to use mysql_num_rows() anymore.

Alternative

According to the new PHP version, you should use mysqli_stmt_num_rows() function. It works like mysql_num_rows() but has better security. The code will be look like below after using mysqli_stmt_num_rows():