mysql - 我的语句返回空行,即使有带有值的记录

标签 mysql sql foreach

我有这个功能:

function get_comments($file_id){

    echo $file_id.'<br /><br />';
    include 'mysql_connect.php'; ($connection is the dblink variable)

    $result = mysqli_query($connection, "SELECT * FROM gastenboek");
    print $row_cnt = mysqli_num_rows($result);
    echo '<br /><br />';

    foreach($result as $item){
        echo $item['comment'];
    }

}

我在 mij db 中创建了 2 条记录,其中包含用于测试的值(也在评论中)。 print $row_cnt 给了我 2 行,但 foreach 语句没有返回任何内容?就像我从语句中得到空行一样?

感谢帮助

最佳答案

执行查询但未获取结果。 将您的代码更新为:

$result = mysqli_query($connection, "SELECT * FROM gastenboek");
print $row_cnt = mysqli_num_rows($result);
echo '<br /><br />';

while($item = mysqli_fetch_assoc($result)){
    echo $item['comment'];
}

关于mysql - 我的语句返回空行,即使有带有值的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34897552/

相关文章:

php - 添加mysql查询到virtualmart

php - 从一张表中抓取Mysql数据并为具有特定ID的特定用户显示它

sql - RETURN 在带有 TRY-CATCH block 的事务内

r - 如何防止嵌套 foreach 循环使用 R 中所有核心的 100% CPU?

c# - 为什么 foreach 在从 ListView 中删除项目时起作用,而在 ListBox 中不起作用?

php - foreach 相当于 jquery 中的 php?

php - DVWA 1.9 设置错误

mysql - 从更新默认值为 CURRENT_TIMESTAMP 的字段中排除某些字段

php - 在 postgreSQL 中运行查询时出现 SQL 错误 42601

php - 使用 PHP 和 MySQL 构建任务列表