1

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

0

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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 1 Here'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());
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 don't you know that strings should be escaped in query?
Yes, I know, thanks...I'm just getting the structure done on my local server
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));
|
1

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.

Comments

0

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.

Comments

0

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.

2 Comments

+1 for suggesting serialisation. However, native serialize would be better in most cases.
+1 for reminding me there's such function. Long time no PHP :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.