我遇到以下代码的问题,它只返回最后1条记录。
使用while循环我能够检索所有值并将其添加到数组并为每个循环获取它但我无法返回所有值。
在调用此函数时,仅返回最后1条记录。任何人都可以帮助我修复此代码,它将返回所有值。感谢
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;
}
答案 0 :(得分:2)
(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;
}
答案 1 :(得分:1)
首先要指出的是mysqli_query()
的第一个参数是你需要用mysqli连接来提供它。
其次,您不需要另一个foreach循环。只需按下while内的值,然后最后返回该值容器。
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);
答案 2 :(得分:0)
为什么再次成为一个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;
}
答案 3 :(得分:0)
循环遍历数组并返回最后一个数组元素
你想要整个数组的位置。
foreach($atradinghours as $atradinghoursr){
$getval = $atradinghoursr;
}
应该只是
$getval = $atradinghours;
return $atradinghours;
答案 4 :(得分:0)
问题出在$getval
,你只是存储最后一个元素,
你需要在所有元素中创建一个数组,push()
,就像你在while循环中所做的那样
并返回数组
或者只返回$atradinghours
,无论如何都是