sqlmysqlisql-deletemulti-query

How to use multi_query with DELETE / UPDATE only?


When I was using multi_query with SELECT, it looked like this:

$sql = 'SELECT....';
$sql .= 'SELECT....';
...

if ($db->multi_query($sql))
{ 
    do
    {
        if ($stmt = $db->store_result())
        {
            while ($row = $stmt->fetch_assoc())
            {
                foreach ($row as $key => $value)
                {
                    $var[$key] = $value;
                }
            }
            $stmt->free_result();
        }
    } while ($db->more_results() && $db->next_result());
}

But how should it look when I need DELETE or UPDATE only, since there are no results?

$sql = 'DELETE...';
$sql .= 'DELETE...';
$sql .= 'UPDATE...';

if ($db->multi_query($sql))
{
    do
    {
        /*well.. nothing?*/
    }
    while ($db->more_results() && $db->next_result());
}

seems to work, even without the do {...}, but isn't there a better / clean solution?


Solution

  • As far as I remember you are on the right way of executing multiple updates. The one thing you seem to miss is checking for errors . For instance (from here),

    if ($db->multi_query($sql))
    {
      do
      {
      // do nothing, just iterate over results to make sure no errors  
      }
      while ($db->next_result());
    }
    if ($db->errno) {
        //error handling
    }