2013-11-02 64 views
-3
$db->beginTransaction(); 
$del = $db->prepare('DELETE FROM hesabat_filter WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$del = $db->prepare('DELETE FROM hesabat_istilik WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$del = $db->prepare('DELETE FROM hesabat_nem WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$del = $db->prepare('DELETE FROM hesabat_rek WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$del = $db->prepare('DELETE FROM hesabat_soyuqluq WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$del = $db->prepare('DELETE FROM hesabat_vent WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$db->commit(); 

$count = $del->rowCount(); 
echo $count; 

儘管此PDO的工作正確,但我無法計算所有刪除的行。我想我應該給每個準備的查詢不同的變量,然後收集變量。你有什麼想法?我應該如何統計PDO中所有已刪除的行?

+1

$每個查詢 –

回答

4
  1. 如果你不使用參數化查詢和不重用查詢每個查詢execution..Try,你不需要prepare()它。你可以query()這樣。
  2. rowCount()只返回最後一個查詢的計數。由於這個原因,並且因爲你一遍又一遍覆蓋了$del,當然你只會得到最後一個查詢的計數。沒有什麼魔法可以執行,您只需在每次查詢後調用rowCount並計算總數即可。但一個query + rowCount代替
  3. ,有PDO::exec,這已經不正是你想要什麼:

    PDO::exec()在一個函數調用執行SQL語句,返回受語句影響的行數。

    $totals = $db->exec('DELETE ...'); 
    $totals += $db->exec('DELETE ...'); 
    ... 
    
    echo $totals; 
    
+1

我應該使用的BeginTransaction()後數+ = $ del-> rowCount時()? – RiKo

+0

如果你想通過交易確保完整性,那麼是的。 – deceze

+0

謝謝!我明白了。 – RiKo

1

計數器加一vairable後這樣

$count = 0; 
$db->beginTransaction(); 
$del = $db->prepare('DELETE FROM hesabat_filter WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$count += $del->rowCount(); 
$del = $db->prepare('DELETE FROM hesabat_istilik WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$count += $del->rowCount(); 
$del = $db->prepare('DELETE FROM hesabat_nem WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$count += $del->rowCount(); 
$del = $db->prepare('DELETE FROM hesabat_rek WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$count += $del->rowCount(); 
$del = $db->prepare('DELETE FROM hesabat_soyuqluq WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$del = $db->prepare('DELETE FROM hesabat_vent WHERE model_id NOT IN (SELECT res_id FROM result)'); 
$del->execute(); 
$count += $del->rowCount(); 
$db->commit(); 

echo $count; 
相關問題