簡體   English   中英

MySQLi結果數組

[英]MySQLi result to array

目前,我的代碼輸出1個數組,但不能全部顯示。 我知道我需要一會兒或foreach循環才能顯示其余內容。

這是我當前的代碼:

/**
 * Get work orders via email and password and institution id
 */
public function getWorkOrdersByInstitution($email, $password, $institution_id) {

    $work_order_stmt = $this->conn->prepare("SELECT * FROM project WHERE institution_id = ?");

    $work_order_stmt->bind_param("s", $institution_id);

    if ($work_order_stmt->execute()) {
        $workorder = $work_order_stmt->get_result()->fetch_assoc();
        $work_order_stmt->close();

       return $workorder;

    } else {
        return NULL;
    }
}

我希望它返回數組。 當前,這是返回的內容(注意:顯示數組時我正在使用json_encode):

{
    "project_id": 1,
    "date": "Undefined",
    "project_code": "ea60f6190c",
    "title": "test",
    "description": "test",
    "demo_url": null,
    "project_category_id": null,
    "client_id": null,
    "company_id": null,
    "staffs": "4,5,",
    "budget": 0,
    "timer_status": 0,
    "timer_starting_timestamp": null,
    "total_time_spent": 0,
    "progress_status": null,
    "timestamp_start": "1516874400",
    "timestamp_end": "1516878000",
    "project_status": 1,
    "project_note": null,
    "institution_id": 12,
    "project_state": 2,
    "strata_manager": 6,
    "site_contact": 7,
    "place_of_service": 18
}

所以最終,我希望所有行都存儲在1個數組中,我可以在另一個頁面上返回並使用它。

是的,您可以使用循環獲取所有結果:

if ($work_order_stmt->execute()) {
    $result = $work_order_stmt->get_result() ;

    $workorders = [] ;
    while ($workorder = $result->fetch_assoc()) {
        $workorders[] = $workorder ;
    }
    $work_order_stmt->close();

   return $workorders;
}
else { .. }

或者,您可以使用fetch_all()獲取所有數據:

$workorders = $result->fetch_all();  

暫無
暫無

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

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