alena1347 Posted March 15, 2013 Share Posted March 15, 2013 Sorry this could sound simple but not getting it. I need help on writing mysql query $sql=mysql_query("SELECT * FROM TABLE WHERE id = ("some array")"); the above query need to be run with id matches with a array of values,the array is say not of definite size. please could someone help Quote Link to comment https://forums.phpfreaks.com/topic/275690-select-query-help/ Share on other sites More sharing options...
Barand Posted March 15, 2013 Share Posted March 15, 2013 example $sql=mysql_query("SELECT * FROM TABLE WHERE id IN (1,2,3)"); $list = join(',', $somearray); $sql = "SELECT * FROM TABLE WHERE id IN ($list)"; Quote Link to comment https://forums.phpfreaks.com/topic/275690-select-query-help/#findComment-1418820 Share on other sites More sharing options...
alena1347 Posted March 18, 2013 Author Share Posted March 18, 2013 example $sql=mysql_query("SELECT * FROM TABLE WHERE id IN (1,2,3)"); $list = join(',', $somearray); $sql = "SELECT * FROM TABLE WHERE id IN ($list)"; But the above will select just id 1 I need the data of all three Quote Link to comment https://forums.phpfreaks.com/topic/275690-select-query-help/#findComment-1419272 Share on other sites More sharing options...
Solution DaveyK Posted March 18, 2013 Solution Share Posted March 18, 2013 As barand stated: $sql=mysql_query("SELECT * FROM TABLE WHERE id IN (1,2,3)"); this will return 3 results (if they exists), where id = 1 OR id = 2 OR id = 3. However, Barand, wouldn't this: $sql = "SELECT * FROM TABLE WHERE id IN ($list)"; require an implode() ? Quote Link to comment https://forums.phpfreaks.com/topic/275690-select-query-help/#findComment-1419321 Share on other sites More sharing options...
Barand Posted March 18, 2013 Share Posted March 18, 2013 Yes. See my reply #2. join and implode are the same Quote Link to comment https://forums.phpfreaks.com/topic/275690-select-query-help/#findComment-1419374 Share on other sites More sharing options...
DaveyK Posted March 18, 2013 Share Posted March 18, 2013 Oh sorry barand, I totally missed that. Quote Link to comment https://forums.phpfreaks.com/topic/275690-select-query-help/#findComment-1419392 Share on other sites More sharing options...
alena1347 Posted March 20, 2013 Author Share Posted March 20, 2013 As barand stated: $sql=mysql_query("SELECT * FROM TABLE WHERE id IN (1,2,3)"); this will return 3 results (if they exists), where id = 1 OR id = 2 OR id = 3. However, Barand, wouldn't this: $sql = "SELECT * FROM TABLE WHERE id IN ($list)"; require an implode() ? thank you was missing something this solved it. Quote Link to comment https://forums.phpfreaks.com/topic/275690-select-query-help/#findComment-1419745 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.