Home  >  Article  >  Backend Development  >  How can we write a PHP script to count the number of rows affected by a MySQL query?

How can we write a PHP script to count the number of rows affected by a MySQL query?

王林
王林forward
2023-08-31 09:09:211017browse

How can we write a PHP script to count the number of rows affected by a MySQL query?

PHP uses the mysql_affected_rows() function to find how many rows were changed by the query. To illustrate this, we have the following example −

Example

<html>
   <head>
      <title>Rows affected by query</title>
   </head>
   <body>
      <?php
         $dbhost = &#39;localhost:3036&#39;;
         $dbuser = &#39;root&#39;;
         $dbpass = &#39;rootpassword&#39;;
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         
         if(! $conn ) {
            die(&#39;Could not connect: &#39; . mysql_error());
         }
         echo &#39;Connected successfully<br />&#39;;
         
         mysql_select_db( &#39;TUTORIALS&#39; );
         $result_id = mysql_query ($query, $conn_id);
         # report 0 rows if the query failed
         $count = ($result_id ? mysql_affected_rows ($conn_id) : 0);
         print ("$count rows were affected</p><p>");
      ?>
   </body>
</html>

The above is the detailed content of How can we write a PHP script to count the number of rows affected by a MySQL query?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:strcmp() function in PHPNext article:strcmp() function in PHP