简体   繁体   中英

How do I get the rows affected by a MySQL query in PHP?

From PHP, if I execute a query like the following with mysqli_stmt_execute :

UPDATE users 
SET hair_color = 'brown' 
WHERE height > 180;

How can I figure out which rows in the table were actually updated and affected by the query?

Please note that I am looking to solve this problem in only PHP, without the use of DB triggers, etc. Very specifically, this question came about because we're trying to remove all triggers from our DB.

$string="UPDATE users SET hair_color = 'brown' WHERE height > 180";
//Update Query String which will perform in update query

$update_query=mysqli_query($connection,$string);
//by this we are actually running our update queries.    

$total_affected_rows=mysqli_affected_rows($con); 
//By this line you will get total affected rows

//if you need to select and show all the affected rows then you can make a 
//query

$sql=mysqli_query($con,"SELECT * FROM users WHERE height > 180");

you can use mysqli_stmt_affected_rows() to get the affected rows

check here http://php.net/manual/en/mysqli-stmt.affected-rows.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM