gamesmstr Posted September 4, 2009 Share Posted September 4, 2009 The problem I have is using a variable array in a MYSQL IN condition. $station_equipment=array(1,2,3,4,5); $weapons=mysql_query("SELECT * FROM equipment WHERE category='weapon' and id IN ('$station_equipment') ORDER BY id ASC"); This returns an empty set where as: $weapons=mysql_query("SELECT * FROM equipment WHERE category='weapon' and id IN (1,2,3,4,5) ORDER BY id ASC"); Works fine. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/ Share on other sites More sharing options...
itaym02 Posted September 4, 2009 Share Posted September 4, 2009 Use the explode function http://php.net/explode to transform the array to a string which will look like that: "1,2,3,4,5,6" Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912411 Share on other sites More sharing options...
gamesmstr Posted September 4, 2009 Author Share Posted September 4, 2009 Explode, transforms Strings into an array of strings, not the other way around, however I'll look at implode and see what that does. Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912414 Share on other sites More sharing options...
kickstart Posted September 4, 2009 Share Posted September 4, 2009 Hi He means implode (and I made exactly the same mistake a couple of days ago). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912415 Share on other sites More sharing options...
gamesmstr Posted September 4, 2009 Author Share Posted September 4, 2009 Nope not working. But now instead of an empty set, it returns only the result from the first element of the array. Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912421 Share on other sites More sharing options...
PFMaBiSmAd Posted September 4, 2009 Share Posted September 4, 2009 You would need to post your current code and an example of what the data in the table looks like. Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912425 Share on other sites More sharing options...
kickstart Posted September 4, 2009 Share Posted September 4, 2009 Hi Is this what you have:- $weapons=mysql_query("SELECT * FROM equipment WHERE category='weapon' and id IN (".explode(',',$station_equipment).") ORDER BY id ASC"); All the best keith Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912430 Share on other sites More sharing options...
gamesmstr Posted September 4, 2009 Author Share Posted September 4, 2009 You would need to post your current code and an example of what the data in the table looks like. You Really don't want me to do that It is huge, complicated, and filled with includes and ajax calls and all of it works. The only thing that doesn't work is the query using IN. It works fine without it, (just doesn't screen the results). It also works fine if I manually add the data in a string. The problem arises when a variable is used with IN. I'll create a simplified section to describe my problem: Pertinent table data: ID = INT(11); Table: equipment ID category 1 weapon 2 weapon 3 weapon 4 weapon 5 weapon <?php include "common/head.php"; $station_equipment=array(1,2,3,4,5); $station_equipment2=implode(",",$station_equipment); $weapons=mysql_query("SELECT * FROM equipment WHERE category='weapon' AND id IN (1,2,3,4,5) ORDER BY id ASC"); $weapons2=mysql_query("SELECT * FROM equipment WHERE category='weapon' AND id IN ('$station_equipment') ORDER BY id ASC"); $weapons3=mysql_query("SELECT * FROM equipment WHERE category='weapon' AND id IN ('$station_equipment2') ORDER BY id ASC"); $result=mysql_num_rows($weapons); $result2=mysql_num_rows($weapons2); $result3=mysql_num_rows($weapons3); echo"result = $result, result2 = $result2, result3 = $result3"; ?> The result is this: result = 5, result2 = 0, result3 = 1 Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912445 Share on other sites More sharing options...
kickstart Posted September 4, 2009 Share Posted September 4, 2009 Hi Remove the single quotes from around $station_equipment2. It is currently trying to find an id that is equal to '1,2,3,4,5' All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912450 Share on other sites More sharing options...
gamesmstr Posted September 4, 2009 Author Share Posted September 4, 2009 That did it! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/#findComment-912454 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.