PHP打印使用While循环和数组返回的所有值
我对以下代码有问题,它只返回最后1条记录 使用while循环,我能够检索所有的值并将其添加到数组中,并为每个循环获取它,但我无法返回所有的值 调用此函数时,只返回最后1条记录。任何人都可以帮助我修复这个代码,它将返回所有的值。谢谢PHP打印使用While循环和数组返回的所有值,php,mysql,arrays,Php,Mysql,Arrays,我对以下代码有问题,它只返回最后1条记录 使用while循环,我能够检索所有的值并将其添加到数组中,并为每个循环获取它,但我无法返回所有的值 调用此函数时,只返回最后1条记录。任何人都可以帮助我修复这个代码,它将返回所有的值。谢谢 echo gettradinghours("54"); function gettradinghours($storeid){ $select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` w
echo gettradinghours("54");
function gettradinghours($storeid){
$select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` where `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');");
$atradinghours = array();
while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
array_push($atradinghours, $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime']);
}
foreach($atradinghours as $atradinghoursr){
$getval = $atradinghoursr;
}
return $getval;
}
为什么又是一个foreach
function gettradinghours($storeid){
$select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` where `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');");
$atradinghours = array();
while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
array_push($atradinghours, $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime']);
}
return $atradinghours;
}
在数组中循环并返回最后一个数组元素 在你想要的地方,整个阵列
foreach($atradinghours as $atradinghoursr){
$getval = $atradinghoursr;
}
应该只是
$getval = $atradinghours;
return $atradinghours;
(1)返回数组:
function gettradinghours($storeid){
$getval = array();
$select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` where `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');");
$atradinghours = array();
while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
array_push($atradinghours, $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime']);
}
foreach($atradinghours as $atradinghoursr){
$getval[] = $atradinghoursr;
}
return $getval;
}
(2)返回字符串:
function gettradinghours($storeid){
$getval = '';
$select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` where `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');");
$atradinghours = array();
while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
$getval.= $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime'];
}
return $getval;
}
问题在于
$getval
您只是在存储最后一个元素
您还需要在所有元素中使其成为一个数组,push()
,就像您在while循环中所做的那样
然后返回数组
或者只需返回所有数据的$atradinghours
- 首先要指出的是,
的第一个参数是您需要为它提供一个mysqli连接mysqli\u query()
- 其次,您不需要另一个foreach循环。只需将值推送到while中,最后返回该值容器
是的,您将在foreach循环完成后返回值,因此它将返回$atradinghours中的最后一个值array@lock,我已经发布了您问题的答案,您只需要在$getval的forloop中添加括号。因此,它将是$getval[]而不是$getval&您的问题将得到解决。rning:mysqli_stmt::bind_result():绑定变量的数量与prepared中的字段数量不匹配statement@lock我做了一些修改,再次查看返回为“Array”的输出结果@lock yes,这是一个数组,使用
var\u dump()/print\u r()
要查看内容,我可以使用print\r()查看输出,但问题是我不希望以数组格式输出,我需要的输出只是字符串格式。输出回显为“array”:(你的意思是,你没有得到数组吗?你可以打印\r(你的返回数组)而不是使用echo作为数组!输出本身被打印为“array”.$valuesArray=gettradinghours(storeid);打印($valuesArray);请按上述注释尝试,您可以获得预期结果。
function gettradinghours($storeid){
// 1st point! connection!
$connection = mysqli_connect('localhost', 'username', 'password', 'database_base');
$select_tradinghours = mysqli_query($connection, "SELECT * FROM `tradinghours` WHERE `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')");
$atradinghours = array();
// no need for that array push thing, just push it normally and return in the end
while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
$atradinghours[] = $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime'];
}
return $atradinghours; // return the gathered/pushed values
}
print_r(gettradinghours("54")); // use the function
function gettradinghours($storeid){
// connection!
$connection = mysqli_connect('localhost', 'username', 'password', 'database_base');
// use prepared statements!
$stmt = $connection->prepare("
SELECT openday, starttime, endtime FROM `tradinghours` WHERE`storeid` = ?
ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')
");
$stmt->bind_param('i', $storeid); // bind the paramter input!
$stmt->execute();
$stmt->bind_result($openday, $starttime, $endtime);
$atradinghours = array();
while($stmt->fetch()) { // as usual push the values
$atradinghours[] = $openday. ' ' .$starttime. ' - ' .$endtime;
}
return $atradinghours; // return the gathered values
}
print_r(gettradinghours(54));
$atradinghours = '';
while($stmt->fetch()) { // as usual push the values
$atradinghours .= $openday. ' ' .$starttime. ' - ' .$endtime . '<br/>';
}
echo gettradinghours(54);