galvin Posted July 26, 2012 Share Posted July 26, 2012 Just looking for insight on the most efficient way to take data in a PHP $_POST array and use it to retrieve further data from a MySQL database. For example, I have a form submitted and on the next page, I end with an array of 10 values, called $_POST['players']. The array looks like this, where each value is simply the ID of each player (for instance, ID 37 is Chris Johnson, ID 10 is Tony Romo, etc). Array ( [0] => 37 [1] => 10 [2] => 39 [3] => 60 [4] => 161 [5] => 217 [6] => 124 [7] => 69 [8] => 136 [9] => 147 ) I just want to know the best way to take that array and use the ID #s (and only those ID #s) to get the full data set for each player from MySQL (Let's just say I have a single table called "players" that has fields of playerid, name, team, position). I would like a final output of data similar to this... Chris Johnson, Tennessee Titans, RB Tony Romo, Dallas Cowboys, QB and so on. I'm very shaky with arrays and I'm sure it's cake to cycle through those specific ten IDs, but I can't figure it out. I THINK I'd have a MySQL statement that essentially says the following... SELECT name, team, position FROM players WHERE playerid = (and this is where I am stuck since I don't know how to get those 10 IDs here) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 26, 2012 Share Posted July 26, 2012 You would implode the array to make a comma separated list and use the mysql IN() comparison in the WHERE clause - http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_in Quote Link to comment Share on other sites More sharing options...
galvin Posted July 26, 2012 Author Share Posted July 26, 2012 Cool, thank you. I just started doing it and I'm getting an "invalid argument passed" error when I try to implode the $_POST['players'] array... $playerids = implode(",",$_POST['players']); Should that work, or is a $_POST array, not a true array (like maybe I have to do something to the posted array first???) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 26, 2012 Share Posted July 26, 2012 If $_POST['players'] is the array that you indicated in the first post, that would work. Have you confirmed what $_POST['players'] actually contains? Is this code inside your form processing code that has detected that the form has been submitted? Quote Link to comment Share on other sites More sharing options...
galvin Posted July 26, 2012 Author Share Posted July 26, 2012 disregard. the arrays were empty because of a stupid typing mistake. it works now. Thanks again Quote Link to comment Share on other sites More sharing options...
galvin Posted July 26, 2012 Author Share Posted July 26, 2012 Does anyone know if the IN () comparison and/or implode automatically eliminate duplicates? I was going to use array_unique to take out any duplicates in my array, but I tested first and the duplicates were removed without even needing array_unique. Just curious. Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 26, 2012 Share Posted July 26, 2012 I don't think it matters. If it matches once, it'll match no matter how many times it's in there. No, implode() does not remove duplicates. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 26, 2012 Share Posted July 26, 2012 The IN() comparison will match the row(s) where the comparison is TRUE. WHERE playerid IN(1,1) is the same as WHERE playerid = 1 OR playerid = 1 and it only returns the one single row with playerid = 1. Quote Link to comment Share on other sites More sharing options...
galvin Posted July 26, 2012 Author Share Posted July 26, 2012 Great thank you so much!!! 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.