但由于某种原因我得到了这个结果:注意:第 20 行 C:\xampp\htdocs\Experime-6ren">
gpt4 book ai didi

php - 函数中的 mysqli_fetch_assoc

转载 作者:行者123 更新时间:2023-11-30 01:01:42 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com