Computer >> Máy Tính >  >> Lập trình >> MySQL

Làm thế nào chúng ta có thể viết tập lệnh PHP để đếm các hàng bị ảnh hưởng bởi truy vấn MySQL?


PHP sử dụng mysql_affected_rows () để tìm ra bao nhiêu hàng mà một truy vấn đã thay đổi. Để minh họa điều đó, chúng tôi có ví dụ sau -

Ví dụ

<html>
   <head>
      <title>Rows affected by query</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'root';
         $dbpass = 'rootpassword';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }
         echo 'Connected successfully<br />';
         
         mysql_select_db( 'TUTORIALS' );
         $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\n");
      ?>
   </body>
</html>