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
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

Link to comment
Share on other sites

$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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.