Unable to save the array in one field.
You need to examine your relational data and make the appropriate changes to your schema. See the example below for a reference to this approach.
If you have to store the array in one field, serialize() and unserialize() will do the trick. But you cannot fulfill requests for actual content.
As an alternative to the serialization function, also json_encode() and json_decode() .
Consider the following array
$a = array( 1 => array( 'a' => 1, 'b' => 2, 'c' => 3 ), 2 => array( 'a' => 1, 'b' => 2, 'c' => 3 ), );
To save it to the database, you need to create a table like this
$c = mysql_connect($server, $username, $password); mysql_select_db('test'); $r = mysql_query( 'DROP TABLE IF EXISTS test'); $r = mysql_query( 'CREATE TABLE test ( id INTEGER UNSIGNED NOT NULL, a INTEGER UNSIGNED NOT NULL, b INTEGER UNSIGNED NOT NULL, c INTEGER UNSIGNED NOT NULL, PRIMARY KEY (id) )');
To work with records, you can perform such requests (and yes, this is an example, beware!)
function getTest() { $ret = array(); $c = connect(); $query = 'SELECT * FROM test'; $r = mysql_query($query,$c); while ($o = mysql_fetch_array($r,MYSQL_ASSOC)) { $ret[array_shift($o)] = $o; } mysql_close($c); return $ret; } function putTest($t) { $c = connect(); foreach ($t as $k => $v) { $query = "INSERT INTO test (id,". implode(',',array_keys($v)). ") VALUES ($k,". implode(',',$v). ")"; $r = mysql_query($query,$c); } mysql_close($c); } putTest($a); $b = getTest();
Connect connect() function returns mysql connection resource
function connect() { $c = mysql_connect($server, $username, $password); mysql_select_db('test'); return $c; }