Solved

Storing an Array

Posted on 2004-10-20
5
179 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

12 Experts available now in Live!

Get 1:1 Help Now