Jump to content

Get actual rows affected after UPDATE


rupam_jaiswal

Recommended Posts

Hi,

I am using the following mysql query statement:

 

mysql_query("UPDATE inventory SET username='$username' WHERE username='$target->username' ORDER BY RAND() LIMIT 15");

 

now I want to also display to the user which rows were affected by the above query and list all those rows in an echo or something.

 

I know i can use mysql_affected_rows() to find HOW MANY rows were affected but can you make a script that will list the rows that were affected? or is it not possible?

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/197456-get-actual-rows-affected-after-update/
Share on other sites

SELECT * FROM inventory WHERE username = '$username'

 

Hi,

There can be a records with username = '$username' even before running the UPDATE query, so  If use SELECT * FROM inventory WHERE username = '$username' then it will fetch all the records which have not been modified by UPDATE command.

I want only the affected rows with the UPDATE command.

Regards

$ids = array();

$query = "SELECT id FROM inventory WHERE username = '$target->username'";
$result = mysql_query($query);
if ($result) {
    while (list($id) = mysql_fetch_row($result)) {
        $ids[] = $id;
    }
}

$query = "UPDATE inventory SET username = '$username' WHERE username = '$target->username'";
$result = mysql_query($query);

echo 'Rows updated: ', implode(', ', $ids);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.