如何仅将multi_query与DELETE / UPDATE一起使用?

时间:2012-08-28 16:26:57

标签: sql mysqli sql-delete multi-query

当我在SELECT中使用multi_query时,它看起来像这样:

$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());
}

但是,当我只需要DELETE或UPDATE时,应该怎么看?因为没有结果?

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

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

似乎也能正常工作,但是没有更好/更干净的解决方案吗?

1 个答案:

答案 0 :(得分:1)

据我所知,您正在以正确的方式执行多项更新。你似乎错过的一件事是检查错误。例如(来自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
}