Shroder
asked on
Storing an Array
I want to store an array within a mysql for future use. I was wondering what the best way of doing this is. I need to be able to retrieve it and be able to use it in its array form after retrieval.
So the key parts are
implode - http://us2.php.net/manual/en/function.implode.php
to join the array together in a string to be inserted into the database
and
explode - http://us2.php.net/manual/en/function.explode.php
to break the string up into an array after you have pulled the string out of the database
implode - http://us2.php.net/manual/en/function.implode.php
to join the array together in a string to be inserted into the database
and
explode - http://us2.php.net/manual/en/function.explode.php
to break the string up into an array after you have pulled the string out of the database
ASKER
Unfortunetly it is a multi demensional array. I just had to make things complicated :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yeah, serializing and unserializing should solve it. it keeps the format and the data exactly the same.
though, when you're inserting it to the DB, i think you should escape the string:
$dbarr = serialize($array);
$sql = "INSERT INTO table (fieldname) VALUES ('".mysql_escape_string($d barr)."'); ";
...
though, when you're inserting it to the DB, i think you should escape the string:
$dbarr = serialize($array);
$sql = "INSERT INTO table (fieldname) VALUES ('".mysql_escape_string($d
...
$array = array(1,2,3,4,5,6,7); //sample array
$db_string = implode("|",$array); //joins array in string like 1|2|3|4|5|6|7
$query = mysql_query("INSERT INTO tablename (fielname) VALUES ('$db_string')") or die(mysql_error());
Then after you pull the data out of the table (supposing the result was in $var)
$restored_array = explode("|",$var); //string from table is exploded back into an array