Jump to content

Advice


Spring

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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%

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.