2014-10-31 48 views
1

我遇到以下只返回最後1條記錄的代碼的問題。PHP打印使用While循環和陣列返回的所有值

使用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

是的,你是在foreach循環完成後返回值,所以它重新在$ atradinghours數組轉換最後一個值 – Choco 2014-10-31 11:32:59

+0

@ lock,我發佈了您的問題的答案,您只需在$ getval的forloop中添加括號即可。所以它會是$ getval []而不是$ getval並且您的問題將得到解決。 – 2014-10-31 11:34:56

回答

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); 
+0

rning:mysq li_stmt :: bind_result():綁定變量的數量不匹配準備語句中的字段數 – lock 2014-10-31 11:41:58

+0

@lock我正在做一些修訂,再次檢查答案 – Ghost 2014-10-31 11:45:31

+0

作爲「數組」返回的輸出 – lock 2014-10-31 11:48:06

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; 
} 
0

您通過數組循環和返回的最後一個數組元素,

在哪裏,只要你想整個陣列。

foreach($atradinghours as $atradinghoursr){ 
       $getval = $atradinghoursr; 
      } 

只應

$getval = $atradinghours; 
return $atradinghours; 
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; 
    } 
+0

輸出被回顯爲「陣列」:( – lock 2014-10-31 11:45:35

+0

)您的意思是說,您沒有得到一個數組?您可以使用print_r(YOUR RETURN ARRAY)不使用echo作爲數組! – 2014-10-31 11:48:03

+0

輸出本身被打印爲「Array」 – lock 2014-10-31 11:48:34

0

問題是在$getval你只是存儲最後一個元素,

,你將需要做出這樣的數組也,像你在while循環

是如何做到的所有元素push()和返回數組

或剛剛返回$atradinghours這是所有的數據無論如何