Question

After executing a couple of mysql queries, how can I determine if all where successful?

I know I can build an array with data ( mysql_error() ) from each mysql_query and then analyze the array, but isn't a better way? Because mysql_affected_rows() isn't the appropriate function to help in this case.

Was it helpful?

Solution

$unsuccessful = 0;

function query($query)
{
    mysql_query($query) or $GLOBALS['unsuccessful']++;
}

// -------------------------
// USAGE:
// -------------------------

query("UPDATE yourTable SET field1 = 'value'");
query("invalid query 1");
query("invalid query 2");
query("UPDATE yourTable SET field2 = 'value'");

if($GLOBALS['unsuccessful'] > 0) 
    echo $GLOBALS['unsuccessful'] .' queries was unsuccessful.'; // prints "2 queries was unsuccessful"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top