0

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.

walrii
  • 3,472
  • 2
  • 28
  • 47
Alex
  • 7,538
  • 23
  • 84
  • 152
  • What do you mean by "successful"? Whether the query succeeded without errors? – deceze Jul 20 '12 at 10:41
  • @deceze, I don't think "successful" can have another meaning but "the action succeeded with no errors" – Alex Jul 20 '12 at 10:44
  • It could also be "it updated the correct rows" or any number of other things, especially since you also mentioned `mysql_affected_rows`. – deceze Jul 20 '12 at 11:10

1 Answers1

2
$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"
Nikola K.
  • 7,093
  • 13
  • 31
  • 39