php - "You have an error in your SQL syntax "升级到 CodeIgniter 3 后

标签 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 文件。

Due to 3.0.0’s renaming of Active Record to Query Builder, inside your config/database.php, you will need to rename the $active_record variable to $query_builder

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

最佳答案

从错误消息看来很明显,不要在 where 中使用 GROUP BY

Codeigniter 的 Active Record对此有支持:

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

关于php - "You have an error in your SQL syntax "升级到 CodeIgniter 3 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28137744/

相关文章:

php - 查找 child 部门的员工 - PHP

javascript - 使用ajax提交表单后无法显示消息

php - 仅使用一个查询返回项目列表中的团队?

PHP 如何在 PayPal 购物车中使用 IPN?

mysql - 为什么where子句中的大于会导致全表扫描?

mysql - 存储过程中的语法错误

php - DB2 PHP 连接

php - 想要使用外键关系获取 Laravel 中当前登录用户的数据,但给出重复的行

mysql - 使用嵌套 MySQL 查询打印一张表中的所有行

php - 如何显示连接表中的图像