Solved

Storing an Array

Posted on 2004-10-20
5
180 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:Shroder
5 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12363486
If its a one dimensional array then the easiest way is like this (for example)

$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
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12363501
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
0
 

Author Comment

by:Shroder
ID: 12363507
Unfortunetly it is a multi demensional array.  I just had to make things complicated :)
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 12363621
use serialize and unserialze function of php. So you can even store objects in the database
See http://de3.php.net/serialize

Before the insert do a
$dbarr = serialize($array);

after you fetch the data from the database do a
$array = unserialize($row['array']);
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 12367732
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($dbarr)."');";
...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now