我想要一个使用 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/