Spring Posted November 29, 2011 Share Posted November 29, 2011 Well, In my DB i have user items as follows: 13,12,11,9,27,15,16,22,21,23,24,26,29,30,31,32,33 Each ID is a user item that has been imploded to be sent to the DB. Well, I', trying to take them out of the DB now, and I'm having a small problem turning them back into integers. Here's what I've done. $items = explode(",", $row['user_item_id']); So I get: 0 => string '13' (length=2) 1 => string '12' (length=2) 2 => string '11' (length=2) 3 => string '9' (length=1) 4 => string '27' (length=2) 5 => string '15' (length=2) 6 => string '16' (length=2) 7 => string '22' (length=2) 8 => string '21' (length=2) 9 => string '23' (length=2) 10 => string '24' (length=2) 11 => string '26' (length=2) 12 => string '29' (length=2) 13 => string '30' (length=2) 14 => string '31' (length=2) 15 => string '32' (length=2) 16 => string '33' (length=2) Then I do this: foreach($items as $item){ var_dump($item); $sql = 'SELECT * FROM `MYTABLE` WHERE item_tab = '.$tab_id.' AND item_id = '.$item.' AND item_pose =0'; } and get this: string '13' (length=2) string '12' (length=2) string '11' (length=2) string '9' (length=1) string '27' (length=2) string '15' (length=2) string '16' (length=2) string '22' (length=2) string '21' (length=2) string '23' (length=2) string '24' (length=2) string '26' (length=2) string '29' (length=2) string '30' (length=2) string '31' (length=2) string '32' (length=2) string '33' (length=2) Now, this works how I want it to except, I want a integer value not a string. What would be the best way to approach this? Quote Link to comment Share on other sites More sharing options...
xyph Posted November 29, 2011 Share Posted November 29, 2011 intval You shouldn't loop queries though, unless you absolutely have to. Since you've stored the values in a comma separated list, rather than in their own rows, you will need to use two queries though. Check out the IN syntax http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in Quote Link to comment Share on other sites More sharing options...
Spring Posted November 29, 2011 Author Share Posted November 29, 2011 intval You shouldn't loop queries though, unless you absolutely have to. Since you've stored the values in a comma separated list, rather than in their own rows, you will need to use two queries though. Check out the IN syntax http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in Highly appreciate the MySQL IN syntax. I have never used it before. I'll try these out and update you accordingly. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2011 Share Posted November 29, 2011 removing quote There are no actual quotes around the values in the array. The var_dump simply shows them that way to indicate where the string value being displayed starts and stops. There's no need to do anything with the array values to use them. If you were converting to json, you might need perform a type conversion, but it is not needed for what you are showing in this example. Quote Link to comment Share on other sites More sharing options...
xyph Posted November 29, 2011 Share Posted November 29, 2011 If you had stored them in their own rows, you could accomplish everything in a SINGLE query. This is the way RDBMS were designed to work SELECT * FROM `MYTABLE` WHERE `item_id` IN ( # Your first query would go here SELECT `id` FROM `itemstable` WHERE `something` = 'foobar' ) AND `item_tab` = '$tab_id' Saves you for having to verify the results of two queries, and is slightly faster removing quote There are no actual quotes around the values in the array. The var_dump simply shows them that way to indicate where the string value being displayed starts and stops. There's no need to do anything with the array values to use them. If you were converting to json, you might need perform a type conversion, but it is not needed for what you are showing in this example. He didn't say anything about quotes, just that he wanted to values to be integers. Though, in PHP, there isn't a big difference (though a few functions are specific about argument types). [edit] thread title [/edit] Quote Link to comment Share on other sites More sharing options...
Spring Posted November 29, 2011 Author Share Posted November 29, 2011 I got it working correctly now! For someone reason, the array didn't have the right keys..anyway, $sql = 'select * from MYTABLE where item_id IN ('.$items.') AND item_tab = "'.$tab_id.'" AND `item_pose` = 0'; Worked fine! 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.