我在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);
}
答案 0 :(得分:0)
PHP的foreach期望param one是一个可迭代的数组,但似乎你传入一个语句对象(printr)将不再输出select语句)。你可能想尝试一下:
$stmt = $db->query($sql);
$stmt->execute();
$results = $stmt->fetchAll();
foreach($results as $result)
var_dump($result);
答案 1 :(得分: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);
}
}