Solved

Storing an Array

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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…

776 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