【问题标题】:Failed to select data with where clause in array使用数组中的 where 子句选择数据失败
【发布时间】:2016-04-21 18:00:47
【问题描述】:

我有一个数组 $DeptID :array(5) { [0]=> string(1) "2" [1]=> string(1) "8" [2]=> string(2) "11" [3]=> string(2) "15" [4]=> string(2) "17" } 然后,我想从 MySQL 数据库中选择以获取数组中 DeptID 的数据。我的查询:

$DeptdID = implode(',', $DeptID);
$this->db->select(*)
->from('tabel_data')
->where('DeptID IN ('.$DeptID.')')
->group_by('DeptID', 'ASC')
->get(''):

但是发生了错误。

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 (Array) GROUP BY `DeptID` at line 6. 

也许有人可以给我一个解决方案

【问题讨论】:

  • 您的意思是 order_by 而不是 group_by?
  • 哦,是的。对不起,实际上,我的查询很长,但我把它删掉了,所以人们可以简单地阅读它
  • 看起来您拼写错误或在 where 子句中引用了错误的变量。尝试在 where() 中引用 $DeptdID 或在第一行中将 $DeptdID 更改为 $DeptID

标签: php mysql arrays codeigniter


【解决方案1】:

由于您使用的是 CodeIgniter,您可以尝试使用 where_in 函数而不是 implodewherewhere_in 方法将为您执行此操作。

$this->db->select(*)
->from('tabel_data')
->where_in('DeptID', $DeptID)
->group_by('DeptID', 'ASC')
->get(''):

【讨论】:

【解决方案2】:

您的$DeptID 是一个数组,当转换为字符串时,它的输出为Array。你需要内爆它:

$DeptID = '(' . implode(',', $DeptID) . ')';

在将其放入查询之前。

【讨论】:

  • 是的,但它仍然无法正常工作。我会更新我的问题
  • implode 函数对于使其正常工作至关重要。 ($DeptdID != $DeptID)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 2015-11-16
  • 2023-02-07
  • 2019-04-10
相关资源
最近更新 更多