开发者

store more than one value into a db field to be retrieved as separate values

开发者 https://www.devze.com 2023-01-23 05:54 出处:网络
Is it possible to store more than one value and query the database and assign each value as a separate entity?

Is it possible to store more than one value and query the database and assign each value as a separate entity?

For example, I have a database with a column wallpaper can I store say this into it.

wall_1.png, wall_2.png, wall开发者_C百科_3.png

and then query the database and pull out 3 different variables that hold the wallpaper name.

so I'd have $defaultWall = "wall_1.png" $wall2 = "wall_2.png" $wall3 = "wall_3.png" <-- being pulled from the database.

also I have a table called settings, the user can set all their settings here. One column is for their wallpaper preference for their profile. I would like for them to have a series of uploaded wallpapers on hand if they choose to change later on. So by doing this I wanted to store an array of wallpaper urls into the db and have a tiny image preview gallery pull each wallpaper for processing and resizing from the database.


Yes, you can. But more than one value in table field doesn't satisfy to the Normalization Forms.

Try to create one table with wallpapers for this row.


The correct way, from DDL logic, is to define another table for wallpaper images, which have foreign keys that link them to the row of that table which has the Wallpaper column in your case.

If you don't find any reason to do so, and you ARE using only PHP, use serialize and unserialize php functions to access that data from the field. You can store any data structures that way.


Take a look at the expand() function in PHP!

http://www.php.net/manual/en/function.explode.php

Note I don't think this is the best way to handle such values, if you want to be able to link any amount of wallpapers (or none) to an object I would choose to use another table.

create table tblMain (ItemId, Name, etc)
create table tblWallpapers (WallpaperId, WallpaperName)
create table tblMainUsesWallpapers (Wallpaperid, ItemId)

then just select the values like so:

select * from tblWallpapers w
inner join tblMainUsesWallpapers muw
on w.WallpaperId = muw.WallpaperId
inner join tblMain m
on muw.ItemId = m.ItemId
where m.ItemId = x
0

精彩评论

暂无评论...
验证码 换一张
取 消