Jump to content

[SOLVED] Update Query Notification


ballouta

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

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.