我在laravel中有这个查询,当它在表中找到数据时运行完全正常但在找不到匹配数据时给出错误,如果在表中找不到数据时如何使查询没有错误?
DB::select(DB::raw('SELECT * FROM time
WHERE p_day = dayname(CURDATE())
AND CURTIME() BETWEEN p_start AND p_end'))[0];
答案 0 :(得分:0)
多数民众赞成因为那时DB :: select返回的数组中没有项目。在使用索引运算符
访问它之前,您必须检查其长度 试试吧......那样:$results = DB::select(DB::raw('SELECT * FROM time
WHERE p_day = dayname(CURDATE())
AND CURTIME() BETWEEN p_start AND p_end'));
if (!empty($results)) // or: if (count($results) > 0)
{
// Stuff was returned
$desiredResult = $results[0];
// Your hork here
}
else
{
// what you need to to if nothing is returned (no matches for your query)...
}