Forum Moderators: coopster

Message Too Old, No Replies

saving a multidimensional array into a mysql database

         

me_great

3:50 pm on Apr 6, 2008 (gmt 0)

10+ Year Member



Hi,

I am trying to save a multidimensional array in a mysql table.

But, it is saving a null value instead.

I have tried this with serialize()as well, but I get "N;" in the database field.

Can you tell me where is the mistake.
Thanks.

ayushchd

6:14 pm on Apr 6, 2008 (gmt 0)

10+ Year Member



Please post your code, so that we can help :)

scriptmasterdel

6:46 pm on Apr 6, 2008 (gmt 0)

10+ Year Member



Yeah, some example code would help.

Usually it's as simple as ...

<?php

$array = array('Some Text', 'Some More Text', 'Yes, you got it ... More Text');

$serialize_array = serialize($array);

mysql_query('INSERT INTO table SET field = "'.$serialize_array.'"');

?>

Then when pulling the data back out of the table, you should do something like this ...

<?

$getRes = mysql_query('SELECT * FROM table');
while ($getRow = mysql_fetch_assoc($getRes)) {
$unserialized_array = unserialize($getRow['field']);
echo $unserialized_array[0]; // Outputs 'Some Text'
}

?>

me_great

8:55 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



Hi scriptmasterdel,

Thank you much for the reply. It helped really.

I have been able to save the array successfully in the DB.
However, The problem us retrieving it.

Can you please let me know, whats wrong in the following code:

<?php
mysql_connect("localhost","root");
mysql_select_db("arr_test");
$doquery=mysql_query("select * from arr_table");
while($getrow=mysql_fetch_array($doquery))
{
$var=unserialize($getrow['arr_all']);
echo $var[0];
}

?>

Because the result I am getting after executing this is simply the word "Array".

mikhaill

3:45 am on Apr 8, 2008 (gmt 0)

10+ Year Member



Because as you mentioned this is a multi-d array thus $var[0] is still an array and you'd need to print_r($var[0]); to see the content of the array so then you can build the proper echo strings.