Current location: Hot Scripts Forums » Other Discussions » Database » [SOLVED] Storing multiple values in the same field


[SOLVED] Storing multiple values in the same field

Reply
  #1 (permalink)  
Old 03-13-08, 02:45 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Storing multiple values in the same field

Hi,
I have a table that I am storing values in (suprisingly ) and I have a field that i will need to seperate multiple values in. I need some sort of system to be able to use so that when i come to search the values, i can do a query without having to read the values 1 by one in php and then search to see if the value is in it. I was thinking of doing something along the lines of storing the data like this in a field

Code:
[VALUE A][VALUE B][VALUE C][VALUE Z]
and then when i wanted to query it use something like
sql Code:
  1. SELECT * FROM `table` WHERE `fieldname` LIKE '%[VALUE C]%'
I hope that is clear enough to explain what it is i am trying to do. Does anyone know of a better way of accomplishing this? Please note that the number of values is not fixed, and the data inside them can contain spaces etc

Thanks in advance

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #2 (permalink)  
Old 03-13-08, 04:06 PM
omniman's Avatar
omniman omniman is offline
Coding Addict
 
Join Date: Aug 2006
Location: Baltimore, MD
Posts: 342
Thanks: 0
Thanked 1 Time in 1 Post
Please don't do that - you're asking for trouble.

Rather, create another table and foreign-key the primary key from the original table. That way you can store an unlimited number of values corresponding to that "field" and not have to worry about it.
__________________
"Political Correctness is a doctrine, fostered by a delusionary, illogical, liberal minority and rabidly promoted by an unscrupulous mainstream media, which holds forth the proposition that it is entirely possible to pick up a turd by the clean end."
Reply With Quote
  #3 (permalink)  
Old 03-13-08, 05:17 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Ominman. Could you be a bit more specific please. I understand that I could creat a new table, and have all the values stored in multiple fields, but the problem is that the number varies. Or am I misunderstanding what it is you are trying to say?

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #4 (permalink)  
Old 03-13-08, 09:04 PM
omniman's Avatar
omniman omniman is offline
Coding Addict
 
Join Date: Aug 2006
Location: Baltimore, MD
Posts: 342
Thanks: 0
Thanked 1 Time in 1 Post
See, that's the thing with having a foreign key relationship. Depending on your business logic, the relationship could be 0-N. So, you could have as few as 0 entries in the 2nd table, or as many as you want. That way, you can treat each entry separately.

I don't know your exact implementation, so let's assume the following case:
You have a user table that stores basic information. You want the ability to store multiple phone numbers for one user, but don't want to limit the amount of numbers. You are suggesting creating a phone field, and put multiple values in that. Rather than doing that, create a separate table, and utilize the primary key from the first table to reference the userID.

Say your user table has the following fields:

userID (primary key)
name
email
address

Your phone table would have something like this:
phoneID (primary key)
userID (foreign key - references user table)
phoneNumber
numberType

Sample data may be as follows:
Code:
User Table:
+++++++++++++++++++++++++++++++++++++++++++++++
userID	name		email			address 
----------------------------------------------------------
1	Steve		steve@steve.com	    123 My St
2	Joe		Joe@steve.com		34 His Place
3	John		test@test.com		333 Here Etc
+++++++++++++++++++++++++++++++++++++++++++++++


Phone Table:
+++++++++++++++++++++++++++++++++++++++++++++++
phoneID		userID		number		type
------------------------------------------------------------
1		1		555-1212	home
2		1		888-9987	cell
3		1		333-4423	work
4		2		223-2342	home
+++++++++++++++++++++++++++++++++++++++++++++++
This way, you can see that phone numbers 1,2, and 3 all belong to userID 1(Steve)

Then, to get all of Steve's phone numbers, you could do the following:

sql Code:
  1. SELECT * FROM phone_table WHERE userID = '1'

This should help - or at least get you started in the right direction. I suggest doing some reading on relational databases and in particular, JOINS (how to select multiple tables at once and "link" together).

Good Luck!
__________________
"Political Correctness is a doctrine, fostered by a delusionary, illogical, liberal minority and rabidly promoted by an unscrupulous mainstream media, which holds forth the proposition that it is entirely possible to pick up a turd by the clean end."
Reply With Quote
  #5 (permalink)  
Old 03-13-08, 09:09 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Ah ok I see what you mean omniman. Thanks for explaining it a bit clearer. I will definitely look into JOINS a bit more. I've heard of them but I'm not all that up on mysql, only the basics of how to use SELECTS

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
storing multiple values in one variable Godwin PHP 2 09-25-06 04:10 PM
Insert multiple values into one field jaydwire2 JavaScript 1 11-02-04 09:47 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
Putting multiple values from list into one field rush989 PHP 1 02-04-04 04:47 AM
multiple values in one field smallbeer PHP 6 12-17-03 04:45 AM


All times are GMT -5. The time now is 05:02 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.