如何修复codeigniter项目中的错误?

时间:2017-10-13 04:21:35

标签: php codeigniter

codeigniter框架中的错误消息:     发生数据库错误

                                            $CI->db->where('id',1);
                    $settings   =   $CI->db->get('settings')->row_array();

                                        $CI->db->where('id',$room_type_id);
                                        $CI->db->select('room_types.*,base_price as price');
                    $room_type  =   $CI->db->get('room_types')->row_array();
                    //echo '<pre>'; print_r($room_type);die;

                                        $CI->db->where('room_type_id',$room_type_id);
                                        $CI->db->select('rooms.*,count(room_no) as total_rooms');
                    $rooms      =   $CI->db->get('rooms')->row_array();
                    $total_rooms    =   $rooms['total_rooms'];
                    //echo '<pre>'; print_r($rooms);die;
                    $begin = new DateTime($check_in);
                    $end = new DateTime($check_out);

                    $interval = DateInterval::createFromDateString('1 day');
                    $period = new DatePeriod($begin, $interval, $end);

                    foreach($period as $dt){
                        $date       =    $dt->format( "Y-m-d" );    
                        $dayno      =    $dt->format( "N" );
                        $day        =    $dt->format( "D" );
                        $day        =   strtolower($day);
                        ///echo $date;die;          
                        //check for room block period

                        if($date >= $settings['room_block_start_date'] && $date <=$settings['room_block_end_date'])
                        {
                            $block_message  =   "Sorry.. No Room Available Between ".date('d/m/Y',strtotime($settings['room_block_start_date']))." to ".date('d/m/Y',strtotime($settings['room_block_end_date']))."  ";
                            $CI->session->set_flashdata('error', $block_message);
                            redirect('');

                        }
                                        $CI->db->where('O.room_type_id',$room_type_id);
                                        $CI->db->where('R.date',$date);
                                        $CI->db->select('R.*,');
                                        $CI->db->join('orders O', 'O.id = R.order_id', 'LEFT');
                        $orders     =   $CI->db->get('rel_orders_prices R')->result_array();
                        //echo '<pre>'; print_r($orders);die;
                        //echo $total_rooms;die; 
                        if($total_rooms > 0){
                            //echo count($orders);die;
                            if(count($orders) >= $total_rooms){
                                $CI->session->unset_userdata('booking_data');
                                $CI->session->unset_userdata('coupon_data');
                                $CI->session->set_flashdata('error', "Sorry.. This Dates Between Rooms Not Available Please Try With Another Date Or Room");
                                redirect('front/book/index'.$query);
                            }else{
                                continue;   // continue loop
                            }
                        }else{
                                $CI->session->unset_userdata('booking_data');
                                $CI->session->unset_userdata('coupon_data');
                                $CI->session->set_flashdata('error', "Sorry.. This Dates Between Rooms Not Available Please Try With Another Date Or Room");
                                redirect('front/book/index'.$query);
                        }
                    }

    return;
}

function check_availability_ajax($check_in,$check_out,$adults,$kids,$room_type_id){
                $query      =   '?date_from='.$check_in.'&date_to='.$check_out.'&adults='.$adults.'&kids='.$kids.'&room_type='; 
                $CI =& get_instance();
                if($check_in==$check_out){
                    $check_out  =   date('Y-m-d', strtotime($check_out.'+ 1 day'));
                }
                                        $CI->db->where('id',1);
                    $settings   =   $CI->db->get('settings')->row_array();

                                        $CI->db->where('id',$room_type_id);
                                        $CI->db->select('room_types.*,base_price as price');
                    $room_type  =   $CI->db->get('room_types')->row_array();
                    //echo '<pre>'; print_r($room_type);die;

                                        $CI->db->where('room_type_id',$room_type_id);
                                        $CI->db->select('rooms.*,count(room_no) as total_rooms');
                    $rooms      =   $CI->db->get('rooms')->row_array();
                    $total_rooms    =   $rooms['total_rooms'];
                    //echo '<pre>'; print_r($rooms);die;
                    $begin = new DateTime($check_in);
                    $end = new DateTime($check_out);

                    $interval = DateInterval::createFromDateString('1 day');
                    $period = new DatePeriod($begin, $interval, $end);

                    foreach($period as $dt){
                        $date       =    $dt->format( "Y-m-d" );    
                        $dayno      =    $dt->format( "N" );
                        $day        =    $dt->format( "D" );
                        $day        =   strtolower($day);

                        if($date >= $settings['room_block_start_date'] && $date <=$settings['room_block_end_date'])
                        {
                            $block_message  =   "Sorry.. No Room Available Between ".date('d/m/Y',strtotime($settings['room_block_start_date']))." to ".date('d/m/Y',strtotime($settings['room_block_end_date']))."  ";
                            return $block_message;


                        }

                                        $CI->db->where('O.room_type_id',$room_type_id);
                                        $CI->db->where('R.date',$date);
                                        $CI->db->select('R.*,');
                                        $CI->db->join('orders O', 'O.id = R.order_id', 'LEFT');
                    $orders     =   $CI->db->get('rel_orders_prices R')->result_array();
                        //echo $total_rooms;die; 
                        if($total_rooms > 0){
                            if(count($orders) > $total_rooms){
                                $CI->session->unset_userdata('booking_data');
                                $CI->session->unset_userdata('coupon_data');
                                return 'Sorry.. This Dates Between Rooms Not Available Please Try With Another Date Or Room';
                            }else{
                                continue;   // continue loop
                            }
                        }else{
                                $CI->session->unset_userdata('booking_data');
                                $CI->session->unset_userdata('coupon_data');
                                return 'Sorry.. This Dates Between Rooms Not Available Please Try With Another Date Or Room';
                        }
                    }

    return 1;
}

为什么会出现这样的错误以及如何消除它?在代码中可能存在什么问题?

在这些功能中,出现问题或一切正常?

功能check_availability($ check_in,$ check_out,$ adults,$ kids,$ room_type_id){                     $ query =&#39;?date_from =&#39;。$ check_in。&#39;&amp; date_to =&#39;。$ check_out。&#39;&amp; adults =&#39;。$ adult。 &#39;&安培;孩子=&#39; $孩子&#39;&安培; room_type =&#39 ;;                     $ CI =&amp; get_instance();                     如果($ CHECK_IN == $ CHECK_OUT){                         $ check_out =日期(&#39; Y-m-d&#39;,strtotime($ check_out。&#39; + 1天&#39;));                     }

function index()
{
    //echo '<pre>'; print_r($_GET);
    //check availbilty
    //get_invoice_number();
    $this->session->unset_userdata('booking_data');
    $this->session->unset_userdata('coupon_data');
    $data['page_title']     = lang('make_reservation');
    $data['meta_description']   =   $this->setting->meta_description;
    $data['meta_keywords']      =   $this->setting->meta_keywords;  
    $data['banners']        = $this->homepage_model->get_banners();
    $data['testimonials']   = $this->homepage_model->get_testimonials();    // get 6 testimonials
    $data['room_types']     = $this->homepage_model->get_room_types_all();
    $data['taxes']          = $this->homepage_model->get_taxes();
        if(!empty($_GET['room_type'])){
            $data['services']           = $this->homepage_model->get_paid_services($_GET['room_type']);
        }
        //echo '<pre>'; print_r($data['services']);
    if(empty($_GET['room_type'])){
        $this->render('book/room_types', $data);        
    }else{
        check_availability($_GET['date_from'],$_GET['date_to'],$_GET['adults'],$_GET['kids'],$_GET['room_type']);

        $data['room_type']      = $this->homepage_model->get_room_type($_GET['room_type']);

        $this->render('book/view', $data);      
    }


}

这是我使用该函数的Book.php控制器代码:

["Microsoft.Website", "Microsoft.Compute"]

2 个答案:

答案 0 :(得分:2)

由于MYSQL中的ONLY_FULL_GROUP_BY选项设置如此友好,您正面临此问题,

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

解决问题。

答案 1 :(得分:1)

包括COUNT()SUM()等聚合函数的SQL查询总是有一个GROUP BY子句。其中指定了最终结果集中的其他非分组列。

在您的查询中,注意到以下注释:

  1. 您已指定rooms.*,在分组时不建议这样做。
  2. 您可以在分组时提及特定列,并在GROUP BY子句中指定这些列。
  3. 例如,

    SELECT
        Count(product_tb.product_id),
        product_tb.`name`,
        product_tb.details
    FROM
        `product_tb`
    WHERE
    product_tb.product_id = 1
    GROUP BY
        product_tb.`name`,
        product_tb.details