Qyouu
没有好的方法可以将数组存储到单个字段中。您需要检查您的关系数据并对模式进行适当的更改。请参阅下面的示例以获取对此方法的参考。如果必须将数组保存到单个字段中,则serialize()and unserialize()函数可以解决问题。但是您不能对实际内容执行查询。作为序列化功能的替代方法,还有 json_encode()和json_decode()。考虑以下数组$a = array( 1 => array( 'a' => 1, 'b' => 2, 'c' => 3 ), 2 => array( 'a' => 1, 'b' => 2, 'c' => 3 ),);要将其保存在数据库中,您需要创建一个这样的表$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) )');要使用记录,您可以执行诸如此类的查询(是的,请注意,请注意!)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()函数返回一个mysql连接资源function connect() { $c = mysql_connect($server, $username, $password); mysql_select_db('test'); return $c;}