簡體   English   中英

使用 CodeIgniter 和 MySQL 從數據庫獲取數據時出錯

[英]Getting error while fetching data from DB using CodeIgniter and MySQL

嘗試使用代碼點火器從數據庫中獲取記錄時出現以下錯誤。

錯誤:

A PHP Error was encountered
Severity: Error

Message: Call to a member function result() on a non-object

Filename: models/Bookings_model.php

Line Number: 1299

Backtrace:

我在下面提供我的查詢。

$this->db->select('pt_bookings.booking_id,pt_bookings.booking_ref_no,pt_bookings.booking_item,pt_bookings.booking_date,pt_bookings.dep_date,pt_bookings.dep_time,pt_bookings.cutoff_time,pt_bookings.city_id,pt_bookings.src_city,pt_bookings.src_lat,pt_bookings.src_long,pt_bookings.booking_user,pt_bookings.booking_status,pt_bookings.booking_total,pt_bookings.booking_adults,pt_bookings.booking_vehicle,pt_bookings.cron_read,pt_accounts.accounts_id,pt_accounts.ai_first_name,pt_accounts.ai_last_name,pt_accounts.accounts_email,pt_accounts.ai_mobile,pt_tours.tour_id,pt_tours.tour_title,pt_tours.tour_type,pt_tours.tour_location,pt_booking_status.id');
$this->db->join('pt_accounts', 'pt_bookings.booking_user = pt_accounts.accounts_id');
$this->db->join('pt_tours', 'pt_bookings.booking_item = pt_tours.tour_id');
$this->db->where('pt_bookings.booking_status', $sta);
$this->db->where('pt_bookings.cron_read', $cron_read);
$tquery = $this->db->get('pt_bookings')->result();

當我獲取值但遇到致命錯誤時,這里使用上述查詢。

嘗試像這樣修改查詢:

    $this->db->select('pt_bookings.booking_id,pt_bookings.booking_ref_no,pt_bookings.booking_item,pt_bookings.booking_date,pt_bookings.dep_date,pt_bookings.dep_time,pt_bookings.cutoff_time,pt_bookings.city_id,pt_bookings.src_city,pt_bookings.src_lat,pt_bookings.src_long,pt_bookings.booking_user,pt_bookings.booking_status,pt_bookings.booking_total,pt_bookings.booking_adults,pt_bookings.booking_vehicle,pt_bookings.cron_read,pt_accounts.accounts_id,pt_accounts.ai_first_name,pt_accounts.ai_last_name,pt_accounts.accounts_email,pt_accounts.ai_mobile,pt_tours.tour_id,pt_tours.tour_title,pt_tours.tour_type,pt_tours.tour_location,pt_booking_status.id');
    $this->db->join('pt_accounts', 'pt_bookings.booking_user = pt_accounts.accounts_id');
    $this->db->join('pt_tours', 'pt_bookings.booking_item = pt_tours.tour_id');
    $this->db->where('pt_bookings.booking_status', $sta);
    $this->db->where('pt_bookings.cron_read', $cron_read);
    $tquery = $this->db->get('pt_bookings');
    if ($tquery->num_rows() > 0) {
        $tquery = $tquery->result();
    } else {
        echo 'no data found.';
    }

因此,當$tquery值為空時,它將返回no data found. 信息。

由於您沒有發布 controller 代碼,因此很難確切知道是什么觸發了錯誤。 但是,這里有一些指示。

1.- $query->result()方法返回一個對象數組,因此無論返回多少行,您都需要考慮數組索引或遍歷返回的對象。

2.- $query->result_array()方法返回一個arrays 數組,因此上述內容也適用。

在您的 controller 中,您應該始終檢查返回的數組是否包含數據。 根據 Hasta Dhana 的建議,返回一串說明“未找到數據”的文本並不是一個好主意,因為它不允許在 controller 中以標准且有效的方式處理此問題。 我的建議:

$this->db->select('pt_bookings.booking_id,pt_bookings.booking_ref_no,pt_bookings.booking_item,pt_bookings.booking_date,pt_bookings.dep_date,pt_bookings.dep_time,pt_bookings.cutoff_time,pt_bookings.city_id,pt_bookings.src_city,pt_bookings.src_lat,pt_bookings.src_long,pt_bookings.booking_user,pt_bookings.booking_status,pt_bookings.booking_total,pt_bookings.booking_adults,pt_bookings.booking_vehicle,pt_bookings.cron_read,pt_accounts.accounts_id,pt_accounts.ai_first_name,pt_accounts.ai_last_name,pt_accounts.accounts_email,pt_accounts.ai_mobile,pt_tours.tour_id,pt_tours.tour_title,pt_tours.tour_type,pt_tours.tour_location,pt_booking_status.id');
    $this->db->join('pt_accounts', 'pt_bookings.booking_user = pt_accounts.accounts_id');
    $this->db->join('pt_tours', 'pt_bookings.booking_item = pt_tours.tour_id');
    $this->db->where('pt_bookings.booking_status', $sta);
    $this->db->where('pt_bookings.cron_read', $cron_read);
    $tquery = $this->db->get('pt_bookings');

    return ($tquery->num_rows() == 0) ? false : $tquery->result();

通過在沒有從數據庫中獲取任何行時返回false ,您的 controller 可以以更加標准和面向未來的方式確定要做什么。 假設您使用$data = $this->your_model->model_method($params); ,你可以在下面做這樣的事情:

if ($data !== false)
{
    // loop through the array or do whatever it is you need to do.
    // since this will happen only when there is actual data being returned, you'll not get the error if no data was fetched
}

暫無
暫無

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

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