Solved

Storing an Array

Posted on 2004-10-20
5
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
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 …

728 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