Spring Posted November 30, 2011 Share Posted November 30, 2011 So here's my problem I'm not sure how to approach this: I have a table with user_items which are stored together separated by commas. 13,12,11,9,27,15,16,22,21,23,24,26,29,30,31,32,33 Now, I have a script where the user is in a trade and I want to verify the item they are trying to trade, but is there an alternative other than grabbing all of that users' items and checking that one item with all of the records? I've tried using SELECT * FROM MYTABLE WHERE user_item_id IN(33) As an example to see if it will pull the rows with that ID. It didn't seem to work, am I doing it wrong? if so, forgive me. Any suggestions/help? The main problem is I don't want to have to explode that data and use a foreach to check that one item against all of that users items, as they could have well over 500. Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/ Share on other sites More sharing options...
Pandemikk Posted November 30, 2011 Share Posted November 30, 2011 I really don't get what you're asking. Are you asking how to fetch user_items that have "33" somewhere in there? You'll have to use LIKE %33% but I recommend you normalize your data. There should be a table for each user and an item they have. They shouldn't all be stored in one table. Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292755 Share on other sites More sharing options...
Spring Posted November 30, 2011 Author Share Posted November 30, 2011 I really don't get what you're asking. Are you asking how to fetch user_items that have "33" somewhere in there? You'll have to use LIKE %33% but I recommend you normalize your data. There should be a table for each user and an item they have. They shouldn't all be stored in one table. my table has id, user_id, User_item_ids and User_item_shop_id Is that bad? And I'll try what you've suggested. Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292759 Share on other sites More sharing options...
requinix Posted November 30, 2011 Share Posted November 30, 2011 my table has id, user_id, User_item_ids and User_item_shop_id Is that bad? Actually yes. And I'll try what you've suggested. I hope you're referring to the normalization. Besides, the LIKE %33% isn't quite right. Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292763 Share on other sites More sharing options...
Pandemikk Posted November 30, 2011 Share Posted November 30, 2011 Yes, it is. It's not logical and that's why you're having a pain working with it. Table should be: user_id, User_item_id, User_item_shop_id With a unique key on user_id and User_item_id EDIT: I'd even take out User_item_shop_id and put it in another table with: item_id, shop_id Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292765 Share on other sites More sharing options...
Spring Posted November 30, 2011 Author Share Posted November 30, 2011 Yes, it is. It's not logical and that's why you're having a pain working with it. Table should be: user_id, User_item_id, User_item_shop_id With a unique key on user_id and User_item_id EDIT: I'd even take out User_item_shop_id and put it in another table with: item_id, shop_id Appreciate it! This isn't my table, I'm working with an old script so any advice helps! Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292767 Share on other sites More sharing options...
Spring Posted November 30, 2011 Author Share Posted November 30, 2011 my table has id, user_id, User_item_ids and User_item_shop_id Is that bad? Actually yes. And I'll try what you've suggested. I hope you're referring to the normalization. Besides, the LIKE %33% isn't quite right. What do you suggest rather than LIKE %33% Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292768 Share on other sites More sharing options...
Drummin Posted November 30, 2011 Share Posted November 30, 2011 <?php //$search=value searching for $search=35; $user_items=array(13,12,11,9,27,15,16,22,21,23,24,26,29,30,31,32,33); $in_array=in_array($search, $user_items); if($in_array==1){ echo "Has item"; } else{ echo "Item not found"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292770 Share on other sites More sharing options...
Pandemikk Posted November 30, 2011 Share Posted November 30, 2011 <?php //$search=value searching for $search=35; $user_items=array(13,12,11,9,27,15,16,22,21,23,24,26,29,30,31,32,33); $in_array=in_array($search, $user_items); if($in_array==1){ echo "Has item"; } else{ echo "Item not found"; } ?> He's searching through a field in a database, not an array. That being said, your method is full of flaws. Please pay attention to the topic at hand. Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292773 Share on other sites More sharing options...
requinix Posted November 30, 2011 Share Posted November 30, 2011 What do you suggest rather than LIKE %33% Normalize the tables as Pandemikk said. Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292774 Share on other sites More sharing options...
Spring Posted November 30, 2011 Author Share Posted November 30, 2011 What do you suggest rather than LIKE %33% Normalize the tables as Pandemikk said. I have done so. Quote Link to comment https://forums.phpfreaks.com/topic/252151-advice/#findComment-1292779 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.