So I tried to loop through my database tabel with a foreach loop, but when I var_dump the function, it gives me an empty array. Here is the code:
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'];
}
Variables $naam
and $title
are undefined in function scope. I suppose you want to pass'em as arguments:
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'];
}