簡體   English   中英

PHP函數無法在foreach中返回MySQL查詢的所有結果

[英]php function not returning all results from a MySQL query in a foreach

大家好,我對從MySQL數據庫檢索數據的函數有一個小問題,然后使用foreach循環遍歷結果,檢查一個值是否為空,如果為空,則將其替換為另一個值。

此功能的問題在於,返回數據后,我只能查看從數據庫檢索到的一條記錄。 可能很簡單,但超出了我。

我想先將其傳遞給控制器​​或視圖。 也許這對於foreach循環是不可能的? 我想念什么?

這是我的代碼示例。

public function get_basic_user_data(){
    $sql = 'SELECT Account.First_Name, Account.Last_Name, Account.User_Name, Profile_Photos.Thumb_Url 
            FROM Account 
            LEFT JOIN Profile_Photos ON Account.idAccount = Profile_Photos.Account_Id 
            AND Profile_Photos.Active = 1
            WHERE Account.idAccount != ?';
    $account_id = $this->get_account_id();
    $data = $this->db->query($sql, $account_id);

    foreach($data->result() as $row){

            if($row->Thumb_Url == NULL){
                $image = base_url().'assets/images/no_photo_thumb.png';
            }else{
                $image = $row->Thumb_Url; 
            }


    $new_data = new stdClass;
    $new_data->First_Name = $row->First_Name;
    $new_data->Last_Name = $row->Last_Name;
    $new_data->User_Name = $row->User_Name;
    $new_data->Thumb_Url = $image;

    }   

    return $new_data;

}   

希望有人可以幫助我嗎? 謝謝!

目前,您只是返回最后一個數據行。 像這樣更改代碼,以從該函數返回所有行的數組:

$rows = array()
foreach($data->result() as $row){

    if($row->Thumb_Url == NULL){
        $image = base_url().'assets/images/no_photo_thumb.png';
    }else{
        $image = $row->Thumb_Url; 
    }


    $new_data = new stdClass;
    $new_data->First_Name = $row->First_Name;
    $new_data->Last_Name = $row->Last_Name;
    $new_data->User_Name = $row->User_Name;
    $new_data->Thumb_Url = $image;

    $rows[] = $new_data;
}   

return $rows;

這樣,從數據庫返回的每一行都將被添加到名為$rows的數組中。 最后,您必須返回新數組。

您每次迭代都覆蓋$ new_data。 嘗試這個

$new_data = new stdClass
...
$all_data[] = $new_data;

您可以只在SQL查詢中使用IFNULL語句,而不用檢查代碼中的空值,這確實將邏輯分開了一點,但是在這種情況下可能是值得的。

該函數僅返回結果的最后一行,因為new_data變量在循環的每個步驟中都被覆蓋。 在函數的開頭聲明new_data一個數組,並將行添加為數組元素

...
$new_data[] = new stdClass;
...

foreach的每次迭代都會覆蓋$ new_data,因此最后函數返回時,將僅返回最后獲取的行。 要返回多個行,可以將所有行存儲在一個數組中,然后最后返回該數組。 它看起來像這樣:

public function get_basic_user_data(){
    $sql = 'SELECT Account.First_Name, Account.Last_Name, Account.User_Name, Profile_Photos.Thumb_Url 
            FROM Account 
            LEFT JOIN Profile_Photos ON Account.idAccount = Profile_Photos.Account_Id 
            AND Profile_Photos.Active = 1
            WHERE Account.idAccount != ?';
    $account_id = $this->get_account_id();
    $data = $this->db->query($sql, $account_id);

    $data = array();
    foreach($data->result() as $row){

        if($row->Thumb_Url == NULL){
            $image = base_url().'assets/images/no_photo_thumb.png';
        }else{
            $image = $row->Thumb_Url; 
        }


        $new_data = new stdClass;
        $new_data->First_Name = $row->First_Name;
        $new_data->Last_Name = $row->Last_Name;
        $new_data->User_Name = $row->User_Name;
        $new_data->Thumb_Url = $image;

        $data[] = $new_data;
    }   

return $data;

}   

為了能夠使用此功能,您必須更改使用該功能循環遍歷對象數組的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM