php 检查是否所有SQL查询都运行无误

scyqe7ek  于 2022-10-30  发布在  PHP
关注(0)|答案(5)|浏览(129)

我在尝试缩短代码,我认为使用数组是最好的方法。
这段代码是由许多不同的查询(mysqli)组成的,如下所示:

$a = $this->db->query("DELETE * FROM ....");
$b = $this->db->query("DELETE * FROM ....");
$c = $this->db->query("DELETE * FROM ....");
$d = $this->db->query("DELETE * FROM ....");
$e = $this->db->query("DELETE * FROM ....");
$f = $this->db->query("DELETE * FROM ....");
$g = $this->db->query("DELETE * FROM ....");
$h = $this->db->query("DELETE * FROM ....");
$i = $this->db->query("DELETE * FROM ....");
$j = $this->db->query("DELETE * FROM ....");
$k = $this->db->query("DELETE * FROM ....");

为了检查此查询是否正确执行,我这样做(因为它们将返回true/false):

if($a && $b && $c && $d && $e && $f && $g && $h && $i && $j && $k){
    //action    
}

但是每次我必须添加一个查询,我必须将它添加到if语句中,它变得很长,我不知道我是否包括了它...
现在我想用数组来做这个:

$a[0] = $this->db->query("DELETE * FROM ....");
$a[1] = $this->db->query("DELETE * FROM ....");
$a[2] = $this->db->query("DELETE * FROM ....");
$a[3] = $this->db->query("DELETE * FROM ....");
$a[4] = $this->db->query("DELETE * FROM ....");
$a[5] = $this->db->query("DELETE * FROM ....");
$a[6] = $this->db->query("DELETE * FROM ....");
$a[7] = $this->db->query("DELETE * FROM ....");
$a[8] = $this->db->query("DELETE * FROM ....");
$a[9] = $this->db->query("DELETE * FROM ....");
$a[10] = $this->db->query("DELETE * FROM ....");

if($a === true){
    //ok
}else{
    //notok;
}

但是这段代码(if语句)不起作用......我做错了什么?

nsc4cvqm

nsc4cvqm1#

请尝试:

$queries=array(
  'DELETE * FROM ....',
  'DELETE * FROM ....',
  'DELETE * FROM ....',
  'DELETE * FROM ....',
);

$is_all_good=true;
foreach($queries as $sql) 
  if(!$this->db->query($sql)) {$is_all_good=false;break;}

echo $is_all_good?'They are all ok.':'They are not all ok.';

如果您的设置允许事务处理,则还可以将此想法与事务处理合并。

$queries=array(
  'DELETE * FROM ....',
  'DELETE * FROM ....',
  'DELETE * FROM ....',
  'DELETE * FROM ....',
);

$this->db->autocommit(false);

foreach($queries as $sql) $this->db->query($sql); 

echo $this->db->commit()?'They are all ok.':'They are not all ok.';
ubof19bj

ubof19bj2#

您可以使用数组来执行此操作,但也可以将所有查询放入事务中,然后检查提交它们是否有效:

$mysqli->autocommit(FALSE);

// put your queries here    

if (!$mysqli->commit()) {
    print("Transaction commit failed");
    exit();
}

通过这种方式,您还可以确保事务中的所有查询都成功(我假设您正在尝试查找或处理这些查询)。如果提交失败,则根本不会执行任何查询(DDL更改语句除外)。
请参阅http://php.net/manual/en/mysqli.commit.php

bis0qfac

bis0qfac3#

请尝试在数组上使用foreach ...

$a[0] = true;
$a[1] = true;
$a[2] = true;
$a[3] = true;
$a[4] = true;
$a[5] = true;

$all_ok=true;
foreach($a as $a_ok)
{
  if(!($a_ok === true))
  {
    $all_ok=false;
    break;
  }
}
bmp9r5qi

bmp9r5qi4#

与其索引实际的数据库查询结果,不如索引查询本身。执行每个查询,如果它是假的,那么我们就知道它们不全是真的。
比如说。

$queries = array(
    'SELECT * FROM ...',
    'SELECT * FROM ...',
    'SELECT * FROM ...',
    'SELECT * FROM ...'
);

// iterate through the queries
foreach ($queries as $query) {
    // execute this query and check the result to be false
    if (!$this->db->query($query)) {
        // handle "not ok" case
        // ...

        // exit the function
        return;
    }
}

// they were all true, so we can handle the "ok" case
// ...
nlejzf6q

nlejzf6q5#

比所提供的解决方案更直接的解决方案:

$all_true = !in_array(false, $a);

这也会更快(迭代将在PHP引擎中完成,而不是在您的代码中),如果您想确保数组只包含布尔值,并且您使用的是兼容的PHP版本:

$all_true = (fn(bool ...$arr) => !in_array(false, $a))(...$a);

相关问题