Jump to content

[SOLVED] array in mysql?


rishiraj

Recommended Posts

I want to create a table having fields username, password and products.

For a single user no of products is not fixed, it varies from 1 to 30.

My first column is username, 2nd is password and in third column i want to add products.

I don't want to make separate columns like product1, product2, product3 because no of product is not fixed.

Is it any alternatives or something like arrays in MySQL that can hold multiple products and later i can chose any of the products by sql query for a user.

 

Thanks in advance for any kind of suggestion.

 

Link to comment
Share on other sites

I want to create a table having fields username, password and products.

For a single user no of products is not fixed, it varies from 1 to 30.

My first column is username, 2nd is password and in third column i want to add products.

I don't want to make separate columns like product1, product2, product3 because no of product is not fixed.

Is it any alternatives or something like arrays in MySQL that can hold multiple products and later i can chose any of the products by sql query for a user.

 

Thanks in advance for any kind of suggestion.

 

Your'e right, you shouldn't make separate columns.. you should make a separate table, and normalize the database.

Link to comment
Share on other sites

These should get you started:

http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html

http://www.sitepoint.com/article/database-design-management

 

A quick example, for your situation would be:

Table: users
| Username | Password | ID

ID; Auto Increment / Primary


Table: products
| Name | User_ID | ID

User_ID; ID of User from table "users"
ID; Auto Increment / Primary

Link to comment
Share on other sites

A friend recommended me to use spaces to separate products and

then use $arr = explode(" ", $res);

to get the different products,

Which method is the best practice, I am beginner so i want to go on right track so in future i don't have to learn basics again.

Link to comment
Share on other sites

What hostfreak is recommended is the best solution you can make with the database. But if you're so obsessed with not creating another table and use a single coloumn, You should use somehing like xml or json notation to encode your data, not space seperated values (use at least commas!!). You may give an array to js_encode() and it will return a string which you can put to the db. While getting it back, you can use js_decode. But I do not recommend it, another table is the right and proper solution.

Link to comment
Share on other sites

You can take a look at PostgreSQL which has an array type (this array type maybe of different types. like char[], numeric[],...). Maybe that may help too. I have never tried that anyway, still use the old and good new table solution. But take a look it might be helpful and may give you another solution :)

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.