2

I have an array $DeptID :array(5) { [0]=> string(1) "2" [1]=> string(1) "8" [2]=> string(2) "11" [3]=> string(2) "15" [4]=> string(2) "17" } Then, I want to select from MySQL database to get data where DeptID in array. My query:

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

But an error occur.

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. 

Maybe someone can give me a solution

4

2 Answers 2

3

Since you're using CodeIgniter you can try using the where_in function instead of implode and where. The where_in method will do this for you.

$this->db->select(*)
->from('tabel_data')
->where_in('DeptID', $DeptID)
->group_by('DeptID', 'ASC')
->get(''):
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. Lots of usefull stuff about active record can be found here for version <= 2.2 and here for version 3+
0

Your $DeptID is an array which when cast as a string comes out as Array. You need to implode it:

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

before putting it in the query.

2 Comments

Yeah, but it still not working. I will update my question
The implode function is kinda critical for getting this to work. ($DeptdID != $DeptID)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.