mysql_affected_rows function

 The PHP mysql_affected_rows function can be used to count the number of record rows affected by the "previous" execution of MySQL syntax. The items that can be counted include INSERT, UPDATE, DELETE and other actions. The function similar to mysql_affected_rows is mysql_num_rows , but mysql_num_rows can only count SELECT The number of rows in the result set.


PHP mysql_affected_rows function syntax
int mysql_affected_rows ( resource )
The resource in the grammar is the MySQL grammar that was executed last or last time. If the mysql_affected_rows function is used without MySQL execution, this function will automatically call mysql_connect to execute. When the MySQL connection cannot be established or the MySQL connection cannot be found, it will E_WARNING level error occurs. On the contrary, if the number of rows affected by MySQL syntax is successfully counted, an integer (int) is returned.

Although the mysql_affected_rows function is used to count the number of rows affected by MySQL syntax, it is very useful, but this function has been deprecated in PHP version 5.5.0 and will be removed in the future, so if the PHP version used is 5.5. For version 0 or newer, you need to use the alternative functions provided by the PHP official. The following two functions are the alternative functions.
  • mysqli_affected_rows
  • PDOStatement::rowCount
PHP mysql_affected_rows function example
<?php
$mysql_link = mysql_connect('localhost','UserName','Password');
if (!$mysql_link) {
 die('Unable to connect to database: '. mysql_error());
}
echo'Affected rows num: '.mysql_affected_rows();
The above example output
Affected rows num: 0
At the beginning of the example, a database connection is established with mysql_connect . If the connection is not established, the following error message may appear
Warning: mysql_affected_rows() [function.mysql-affected-rows]: A link to the server could not be established in ...
Now that the MySQL connection is established, let the example directly use the mysql_affected_rows function to count without any DELETE, UPDATE, INSERT, etc. actions. In the end, because there is no effect on the number of rows, it returns a result like the integer 0.

Post a Comment

0 Comments