views:

74

answers:

4

I'd like to store the following array in a single text field in a mySQL table:

$user_rating[0] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 1', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[1] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 2', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[2] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 3', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[3] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 4', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[4] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 5', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[5] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 6', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[6] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 7', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

I believe I need to implode the associative arrays as well to accomplish this but am having some trouble.

Any other/better array structure solutions are also welcomed.

Thanks

A: 

Have you considered JSON? It might be your best bet if you want to stick to a single field.

PHP - JavaScript Object Notation

edit: Or even better, read (and vote) for the comment below, because serialize is a native function.

methode
+1 for suggesting serialisation. However, native [`serialize`](http://php.net/manual/en/function.serialize.php) would be better in most cases.
lonesomeday
+1 for reminding me there's such function. Long time no PHP :)
methode
A: 

You can use PHP's serialize to convert the array to a single string. When you retrieve it from the database you can use unserialize to convert it back to the array.

Alec
A: 

There is a better solution for sure.
Store this data as a separate records in a dedicated table.
That's what relational databases are for.

Col. Shrapnel
+2  A: 

First, you don't need to specify ordered numeric keys, just write:

$user_rating[] = array();

Second, why would you want to store the entire array as a string? Why not just store each of its fields as s column? Is this not an option?

Third, you can store serialized arrays (or any data, for that matter) in the DB.

serialize($user_rating);
store_to_db();
...
$handler = unserialize(get_data_out());

If you want to store it as a straight up string, however, that's a bit more complicated. You would have to cycle through the array and append the keys and values individually.

tandu
Thanks. I actually tried to store it as serialized data but receive the following error message:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's time to begin the process of dominating your market. Don't just settle for thi' at line 1Here's the code:$serialized_user_rating = serialize($user_rating); $q = "INSERT INTO marketing_roadmap (user_name, roadmap) VALUES ('$user_name', '$serialized_user_rating')";mysql_query($q) or die(mysql_error());
Kenny Powers
I'm not planning to store all the data in the table because it's static (Same for each user). Basically, I just need to store/save the ORDER of the categories for each user.
Kenny Powers
@Kenny don't you know that strings should be escaped in query?
Col. Shrapnel
Yes, I know, thanks...I'm just getting the structure done on my local server
Kenny Powers
yes, you must escape the data first. I'm used to this being a natural occurrence by use of a DB wrapper. Not mandatory, but suggested. $serialized = mysql_real_escape_string(serialize($data));
tandu
Thanks, it works now.
Kenny Powers
@Kenny It seems you don't if you got this error. That's real value of your database basics knowledge. Which you'd better to learn instead of doing such silly mistakes like storing serialized data in the database.
Col. Shrapnel
@Col. Scrapnel I would respect your response if you actually contributed something useful to this question.
Kenny Powers
I think what he is trying to say is that storing serialized data in such a way is considered bad practice. People really love to tell you you're doing things wrong! Stop doing it wrong! Seriously though, if the data is serialized and stuffed in the DB arbitrarily, you can't really do any sort of manipulation on it independently. You just have this data blob that needs its own specific deserialization handling and parsing to be of any use.
tandu
Lol. That makes sense. Thanks for your help @tandu
Kenny Powers
@Kenny sounds sternly, hehe. well, you will harm your app by this, not me :)
Col. Shrapnel