2014-03-28 62 views
1

我試圖縮短我的代碼,我認爲使用數組是最好的方法。PHP:檢查數組是否全部爲真

此代碼是由非常多不同的查詢(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 ...."); 

要查看該查詢正確執行我這樣做(因爲他們將返回真/假):

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語句)不工作... 我在做什麼錯?

+0

因此,如果查詢執行時假定'$ this-> db-> query()'返回true(或真值)是否正確? –

+0

@EdCottrell對!將返回true/false。 –

回答

2

嘗試:

$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.'; 
0

嘗試使用陣列上的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; 
    } 
} 
1

你可以用一個數組做到這一點,但你也可以乾脆把所有查詢到一個事務中,並檢查是否提交他們的作品:

$mysqli->autocommit(FALSE); 

// put your queries here  

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

這樣,你還要確保所有在交易中你的查詢都成功(這是我假設你正在試圖找出或處理)。如果提交失敗,則根本不執行任何查詢(DDL更改語句除外)。

http://php.net/manual/en/mysqli.commit.php

0

而非索引實際的數據庫查詢結果的,你應該索引查詢本身。執行每個查詢,如果它是假的,那麼我們知道它們並不全是真的。

例如。

$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 
// ...