What is a good way to save an array of data to a single mysql field?
Also once I query for that array in the mysql table, what is a good way to get it back into array form?
Is serialize and unserialize the answer?
feedback
|
There is no good way to store an array into a single field. You need to examine your relational data and make the appropriate changes to your schema. See example below for a reference to this approach. If you must save the array into a single field then the As an alternative to the serialization function there is also Consider the following array
To save it in the database you need to create a table like this
To work with the records you can perform queries such as these (and yes this is an example, beware!)
The
|
|||||||
feedback
|
Serialize and unserialize are pretty common for that. You could also use JSON via json_encode and json_decode for a less PHP-specific format. |
|||
feedback
|
Generally, yes, serialize and unserialize are the way to go. If your data is something simple, though, saving as a comma-delimited string would probably be better for storage space. If you know that your array will just be a list of numbers, for example, then you should use implode/explode. It's the difference between If not, then serialize and unserialize work for all cases. |
|||||
feedback
|
Yup, serialize/unserialize is what I've seen the most in many open source projects. |
|||
feedback
|
check out the implode function, since the values are in an array, you want to put the values of the array into a mysql query that inserts the values into a table.
If the values in the array are text values, you will need to add quotes
Also, if you don't want duplicate values, switch the "INto" to "IGNORE" and only unique values will be inserted into the table. |
||||
feedback
|
Uhh, I don't know why everyone suggests serializing the array. I say, the best way is to actually fit it into your database schema. I have no idea (and you gave no clues) about the actual semantic meaning of the data in your array, but there are generally two ways of storing sequences like that
This way you are storing your array in a single row.
The disadvantage of the first method is, obviously, that if you have many items in your array, working with that table will not be the most elegant thing. It is also impractical (possible, but quite inelegant as well - just make the columns nullable) to work with sequences of variable length. For the second method, you can have sequences of any length, but of only one type. You can, of course, make that one type varchar or something and serialize the items of your array. Not the best thing to do, but certainly better, than serializing the whole array, right? Either way, any of this methods gets a clear advantage of being able to access an arbitrary element of the sequence and you don't have to worry about serializing arrays and ugly things like that. As for getting it back. Well, get the appropriate row/sequence of rows with a query and, well, use a loop.. right? |
|||||||||||||||
feedback
|
Just use the serialize PHP function:
However if you are using simple arrays like that you might as well use implode and explode. |
|||
feedback
|
Instead of saving it to the database, save it to a file and then call it later. What many php apps do (like sugarcrm) is to just use var_export to echo all the data of the array to a file. This is what I use to save my configurations data:
I think this is a better way to save your data! |
|||||
feedback
|
The best way, that I found to myself is save array as data string with separator characters
You can then search data, stored in your array with simple query
use explode() function to convert "array_data" string to array
note that this is not working with multidimensional arrays and make sure that your "array_separator" is unique and had not exist in array values. Be careful !!! if you just will take a form data and put in database, you will be in trap, becous the form data isn't SQL-safe ! you must handle your form value with mysql_real_escape_string or if you use MySQLi mysqli::real_escape_string or if value are integer or boolean cast (int) (boolean) on them
|
||||
feedback
|
I would suggest using implode/explode with a character that you know will not be contained in any of the individual array items. Then store it in SQL as a string. |
|||||
feedback
|
As mentioned before - If you do not need to search for data within the array, you can use serialize - but this is "php only". So I would recommend to use json_decode / json_encode - not only for performance but also for portability ! |
|||
feedback
|
Serialize/Unserialize array for storage in a DB Visit http://php.net/manual/en/function.serialize.php From the PHP Manual: Look under "Return" on the page Returns a string containing a byte-stream representation of value that can be stored anywhere. Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field. |
|||
feedback
|