所以我尝试使用foreach循环遍历我的数据库tabel,但是当我var_dump函数时,它给了我一个空数组。这是代码:
function getComment() {
$db = connectDB();
$sql = "SELECT `naam`, `title` FROM `comments` WHERE `naam`=:naam AND `title`=:title";
$stmt = $db->prepare($sql);
$stmt->bindParam(':naam', $naam);
$stmt->bindParam(':title', $title);
$stmt->execute();
return $stmt->fetchAll();
}
foreach (getComment() as $value) {
echo $value['naam'];
echo $value['title'];
}
答案 0 :(得分:1)
变量$naam
和$title
在功能范围内未定义。我想你想传递' em作为参数:
function getComment($naam, $title) {
$db = connectDB();
$sql = "SELECT `naam`, `title` FROM `comments` WHERE `naam`=:naam AND `title`=:title";
$stmt = $db->prepare($sql);
$stmt->bindParam(':naam', $naam);
$stmt->bindParam(':title', $title);
$stmt->execute();
return $stmt->fetchAll();
}
$naam = 'some naam';
$title = 'some title';
foreach (getComment($naam, $title) as $value) {
echo $value['naam'];
echo $value['title'];
}