Jump to content

Recommended Posts

mysql_affected_rows() tells you exactly that - if any rows were affected/updated. But, if the value is 0 that does not necessarily mean the query failed. The update query could have parameters such that the results are zero.

 

For example, you may have a function to soft delete records that are x days old. If you ran it once and then try and run it again, the results of affected rows will be zero, but it did run successfully.

 

So, use mysql_affected_rows() if you need to know if any records were uodated, but to see if the query ran succesfully you just need to test the query. Either this

if (mysql_query($updateQuery)) {
    //success
} else {
    //failure
}

 

Or this:

$result = mysql_query($updateQuery;
if ($result) {
    //success
} else {
    //failure
}

Well an UPDATE query won't return any results. For a SELECT query you would do a test of mysql_num_rows().

 

Example:

//Prepare the query
$query = "SELECT field1, field2, filed3 FROM tableName WHERE field1 = 5";
//$run the query, assigning to a variable
$result = mysql_query($query);

//Validate that query ran
if(!$result)
{
    //Query failed, add error handling
}
else
{
    //Check that there were results
    if (mysql_num_rows($result)==0)
    {
        //There were no results
    }
    else
    {
        //There were results, process the result set
    }
}

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.