当我遍历数据库表格时,Foreach返回空

时间:2017-11-04 19:12:11

标签: php sql

所以我尝试使用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'];
}

1 个答案:

答案 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'];
}