2

I'm have a problem when I want to query to the database , in this case I want to using IN() mysql function for searching data by value field on array.

example I have array from php

$data = array(1,2,3,4);

and this is my query code to selection data on table Employee

$query = 'SELECT * FROM employee_tbl 
    -> WHERE daily_typing_pages IN ' .$data.;

when I'm execute that code I have error like this

Syntax error or access violation: 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 'Array 
SELECT * FROM employee_tbl 
        -> WHERE daily_typing_pages IN Array;

I think that error is come because Array read as string,and don't parse to this format (1,2,3,4)

how i can convert to that format ? any suggestion?

4 Answers 4

2

Use implode() to create an actual comma-separated list of items as a string.

$query = 'SELECT * FROM employee_tbl 
    -> WHERE daily_typing_pages IN (' . implode(",", $data) . ")";
Sign up to request clarification or add additional context in comments.

Comments

2

Try,

$data = array(1,2,3,4);
$ids = join(',',$data);  
$sql = "SELECT * FROM employee_tbl WHERE daily_typing_pages IN ($ids)";

Comments

1

Use implode()

$x = array(1,2,3,4);
$y = implode(',',$x);

Comments

1

Try this code, use implode() to converts array into string

 $query = "SELECT * FROM employee_tbl 
        -> WHERE daily_typing_pages IN  ( ".implode(',',$data)." ) ";

Comments

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.