Storing PHP multi dimentional arrays in MySQL?

Associate
Joined
21 Oct 2002
Posts
306
Location
Devon
So i've scripted a bit of PHP that uses a rather large multi dimentional array(at least 50 arrays with 10 plus values in each) and need to store it in a single entery in a MySQL table. How do I get the data into database and reconstruct the data into a PHP array from the MySQL database. I've had a little google for it and found something about seirializing (sp?) but many people are saying there are better ways.
 
Serialising is fine so long as you're just using mySQL as storage—that is, if you don't want to search the database or order the results by any of the data in the array etc.

Example:

Code:
$array = array('foo' => 'bar', 'baz' => array('lol' => 'ok'));

mysql_query(' INSERT INTO table (data) VALUES("' . serialize($array) . '") ');

Then to fetch:

Code:
$data = array();

$result = mysql_query("SELECT data FROM table");

while ( $row = mysql_fetch_array($result) ) {
    $data[] = unserialize($row['data']);
}

...which would populate $data with all the arrays in the table, though obviously you can do whatever you want with it.
 
Quite often PHP tutorials are created by people who have only just learnt the subject themselves, which makes it quite difficult to explain well what it is they are doing :p
 
Back
Top Bottom