使用CodeIgniter和MySQL从数据库中获取数据时出错

时间:2019-11-21 09:11:55

标签: php mysql codeigniter

尝试使用代码点火器从数据库中获取记录时出现以下错误。

错误:

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();

在获取值但遇到致命错误时,使用上面的查询。

2 个答案:

答案 0 :(得分:0)

尝试像这样修改查询:

embed = var2

因此,当var1 = discord.Embed( title = 'First embed', description = 'Description', colour = discord.Colour.orange() ) var2 = discord.Embed( title = 'Second embed', description = 'Description', colour = discord.Colour.orange() ) var3 = discord.Embed( title = 'Third embed', description = 'Description', colour = discord.Colour.orange() ) msg = await client.say(embed = var1) await asyncio.sleep(1.5) await client.edit_message(msg, embed = var2) await asyncio.sleep(1.5) await client.edit_message(msg, embed = var3) 值为空时,它将返回 $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.'; } 消息。

答案 1 :(得分:0)

由于您没有发布控制器代码,因此很难确切知道是什么触发了错误。但是,这里有一些指针。

1.- $query->result()方法返回一个对象数组,因此,无论返回多少行,您都需要考虑数组索引或遍历返回的对象。

2.- $query->result_array()方法返回一个数组数组,因此以上内容同样适用。

在控制器中,您应始终检查返回的数组是否包含数据。按照Hasta Dhana的建议,返回一串说明“未找到数据”的文本不是一个好主意,因为它不允许在控制器中使用标准有效的方法来处理此问题。我的建议:

$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,您的控制器可以以一种更加标准且面向未来的方式来确定要执行的操作。假设您使用$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
}