2015-09-07 53 views
-3

我在的foreach在PHP得到這個錯誤:腓的foreach錯誤無效參數

在...

代碼的foreach()提供的參數無效:

$database = new PDO('mysql:host=localhost;dbname=staff_tool', $user, ''); 
foreach ($database->query('SELECT id,firstname,lastname FROM `staff` SET categorie_bit = 0') as $testx) { 
       var_dump($testx); 
      } 

編輯代碼:

foreach ($database->query('SELECT id,firstname,lastname,categorie_bit FROM `staff` WHERE categorie_bit = 1 SET categorie_bit = 0') as $testx) { 
      var_dump($testx); 
     } 
+1

並查詢返回任何東西? –

+0

不只是警告信息 – as21

+1

您的新PDO是否返回任何內容?因爲我看不到任何錯誤檢查... – Amarnasan

回答

0

PHP的foreach期望param之一是一個可迭代的數組,但它似乎像傳遞一個語句對象(printr)將不會輸出更多的選擇語句)。你可能會想嘗試:

 $stmt = $db->query($sql); 
     $stmt->execute(); 
     $results = $stmt->fetchAll(); 

     foreach($results as $result) 
       var_dump($result); 
0
$result = $database->query('SELECT id,firstname,lastname,categorie_bit FROM `staff` WHERE categorie_bit = 1 SET categorie_bit = 0'); 

// always check if you have a valid array ... you do not have any guarantees that SELECT query will return you a result set 
if(is_array($result) && count($result) > 0) { 
    foreach ($result as $testx) { 
     var_dump($testx); 
    } 
} 
+0

試過這個去了我在代碼中添加的其他條件 – as21

相關問題