"您的SQL语法错误"升级到CodeIgniter 3之后

时间:2015-01-25 14:53:05

标签: php mysql codeigniter

将我的网站更新为CodeIgniter 3后,我收到了此错误

我的代码:

  public function get_pickedItems(){
    $this->db->select('p.user_id,u.user_name,u.email,u.full_name,u.thumbnail,u.address,u.phone_no,u.postal_code,u.state,u.country,u.city');
    $this->db->from(USER_PAYMENT.' as p');
    $this->db->join(USERS.' as u' , 'p.user_id = u.id');
    $this->db->join(PRODUCT.' as pd' , 'pd.id = p.product_id'); 
    $this->db->where('p.user_id > 1 GROUP BY p.user_id');
    $this->db->order_by('p.id','ASC');
    return $this->db->get();
  }

错误

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`p`.`user_id` ORDER BY `p`.`id` ASC' at line 5

SELECT `p`.`user_id`, `u`.`user_name`, `u`.`email`, `u`.`full_name`, `u`.`thumbnail`, `u`.`address`, `u`.`phone_no`, `u`.`postal_code`, `u`.`state`, `u`.`country`, `u`.`city` FROM `shopsy_user_payment` as `p` JOIN `shopsy_users` as `u` ON `p`.`user_id` = `u`.`id` JOIN `shopsy_product` as `pd` ON `pd`.`id` = `p`.`product_id` WHERE `p`.`user_id` > `1 GROUP BY` `p`.`user_id` ORDER BY `p`.`id` ASC

Filename: models/User_model.php

Line Number: 462

我改变的唯一与数据库相关的是config / database.php文件。

  

由于3.0.0将Active Record重命名为Query Builder,因此在您的内部   config / database.php,您需要重命名$ active_record   变量到$ query_builder

$active_group = 'default';
// $active_record = TRUE;
$query_builder = TRUE;

2 个答案:

答案 0 :(得分:2)

如果您查看完整查询 - 您的分组错位。

 $this->db->group_by('p.user_id'); 

答案 1 :(得分:2)

从错误消息中可以看出非常明显,不要在GROUP BY中使用where

Codeigniter的Active Record支持:

$this->db->group_by('p.user_id');