php - 函数中的 mysqli_fetch_assoc

标签 php mysql mysqli fetch

我想要一个使用 mysqli_fetch_assoc() 获取的函数,它是这样的:

<?php
$con = mysqli_connect("localhost", "root", "", "primary");
function fetchAssoc($query){
    global $con;
    $queryexec = mysqli_query($con, $query);
    $return = array();
    while($stuff = mysqli_fetch_assoc($queryexec)){
        $return[] = $stuff;
    }
    return $return;
}
$yup = fetchAssoc("SELECT * FROM posts");
while($thing = $yup){
    echo $thing, "<br />";
}
?>

但由于某种原因我得到了这个结果: 注意:第 20 行 C:\xampp\htdocs\Experiments\index.php 中的数组到字符串转换 数组

第 20 行是 echo $thing, "<br />";

最佳答案

$yup = fetchAssoc("SELECT * FROM posts");
while($thing = $yup){
    echo $thing, "<br />";
}

$yup 是一个关联数组,在 while 循环中,您将分配给 $thing - 现在它也是一个数组

你应该做类似的事情

foreach($yup as $y){

  // here $y is an array in the form of 
  // array( ['column_name'] => 'value', '[other_col]' => 'value')
  // so you could do 
  echo $y['column'], $y['other_col'],'<br />';

}

关于php - 函数中的 mysqli_fetch_assoc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064441/

相关文章:

sql - 如何使用 CodeIgniter 运行 mysqli 查询?

php - 查看与您的 MariaDB 服务器版本对应的手册,了解在 '"Array"SQL 附近使用的正确语法

php - Composer 自动加载来自外部供应商的类

php - 从 MySQL 查询中获取行数组?

mysql - 如何使用 mysql 创建集中式数据库并链接到 netbeans 中的前端

php - 在选择选项中显示两个表选择值

php - 如何使用 Windows Azure WebJobs 访问我网站的 PHP 文件?

php - 如何让用户打印和下载MySQL查询结果

php - 从数据库获取数据以创建表 - PHP

php - Mysqli 绑定(bind)参数更新