krispykreme Posted February 8, 2008 Share Posted February 8, 2008 lets say i have an array of something like $users[]=123; $users[]=126; $users[]=323; this array can be an infinite amount of size in theory but 99% will be under 50 in size i want to do something like... $query=mysql_query("SELECT rank,score FROM users WHERE userid='$users[]' order by rank ASC"); now this isnt the correct syntax to use, but the hard part im getting at is that i need this information sorted other wise i could just do multiple queries in a for statement going through the array is it possible to do anything like what i want? Quote Link to comment Share on other sites More sharing options...
toplay Posted February 8, 2008 Share Posted February 8, 2008 ...WHERE userid IN (' . join (',', $users) . ') order by rank ASC' Quote Link to comment Share on other sites More sharing options...
krispykreme Posted February 8, 2008 Author Share Posted February 8, 2008 hmmm is there a certain version of mysql to allow you to do this? currently this just shows no information doesnt error or anything from what i can see before it does the join thing in the query i have it print the array and it shows Array ( [0] => 772474564 [1] => 1414350111 [2] => 20707488 ) all of which are in the database so it should be finding these i believe instead of nothing Quote Link to comment Share on other sites More sharing options...
toplay Posted February 8, 2008 Share Posted February 8, 2008 You have numeric customer id's that big, wow. Well, I assume your table column can handle it. Well, put displays to show the full query before it's executed. Take that query and run it outside of PHP, like using myphpadmin. Post actual code and make sure you're properly checking for any MySQL errors (after every call). Display query: <?php $arrUsers[] = 772474564; $arrUsers[] = 1414350111; $arrUsers[] = 20707488; $sql = 'SELECT `rank`, `score` FROM `users` WHERE `userid` IN (' . join (', ', $arrUsers) . ') ORDER BY `rank` ASC'; echo $sql; ?> Above code would produce this query, which you can use to run outside of PHP to see if it works. SELECT `rank`, `score` FROM `users` WHERE `userid` IN (772474564, 1414350111, 20707488) ORDER BY `rank` ASC Quote Link to comment Share on other sites More sharing options...
krispykreme Posted February 8, 2008 Author Share Posted February 8, 2008 alright thanks! looks like my quotes were messing it up it seemed and your example method worked perfectly! do you happen to know the limits of this? like can the array be of inifinite size? also if userid is indexed will it run optimized still? since these are primary keys that cant exist more than once and only examine those rows instead of the whole table? guess i could say examine haha Quote Link to comment Share on other sites More sharing options...
krispykreme Posted February 8, 2008 Author Share Posted February 8, 2008 looks like it is still optimized using that one key Quote Link to comment Share on other sites More sharing options...
fenway Posted February 9, 2008 Share Posted February 9, 2008 IN() clauses are basically ORs... in 4.1, no index will be used; in 5.0, I believe index_merged solves this. Quote Link to comment 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.