tags:

views:

41

answers:

3

i got have 3 id in a string

let $x="6,3,5" now i want to get all color information from tbl_color where color id are 6, 3 and 5.

i made this query, whats wrong with this

$sql=" SELECT * FROM tbl_color WHERE color_id IN(".explode(',',$x).")

please suggest right query

+5  A: 

Hi ILP,

explode() takes a string and turns it into an array. You already have a string. All you need to do is change your statement and just include $x in your string. You don't need to explode it.


UPDATE:

Per your comment, here is how I would do it:

$x="3,4,5";
$sql=" SELECT * FROM tbl_color WHERE color_id IN(".$x.");";

HTH,

-aj

AJ
i need all data from tbl_color where color id is 3 and 4 and 5 then wahta will be exact query, please write down sir
diEcho
How can an ID be 3, 4, and 5 at the same time?
Pekka
not same time sir, i means i got id lists from another query, now i want to find all color name and decriprion of these ids
diEcho
+1  A: 

You are concatenating an array with a string. The explode is not needed:

$sql = "SELECT * FROM tbl_color WHERE color_id IN ($x)";

A full example assuming you get the input from a user or it is sent from the browser to the server:

$x = $_GET['colors']; // 6,3,5
$x = mysql_real_escape_string( $x ); // Prevent SQL Injection attack
$sql = "SELECT * FROM tbl_color WHERE color_id IN ($x)";
Doug Neiner
A: 

If $x is a string, you don't need to use explode, juse use it as is

$sql="SELECT * FROM tbl_color WHERE color_id IN($x)";
gregseth