如何将php数组存储到mysql数据库中

时间:2010-11-27 10:45:22

标签: php

<?PHP

// primarily a method for storing data
// arrays are counted from 0

$hosts = array(
array("ronmexico.kainalopallo.com/","beforename=$F_firstname%20$F_lastname&gender=$F_gender","Your Ron Mexico Name is ","/the ultimate disguise, is ([^<]+)<\/b><\/u>/s"),<u><b>([^<]+)<\/b><\/u>/s"),

array("rumandmonkey.com/widgets/toys/mormon/index.php","gender=$F_gender&firstname=$F_firstname&surname=$F_lastname","Your Mormon Name is ","/
My <p>My Mormon name is
<b>([^<]+)<\/b>!<br \/>/s")
);

return $hosts;

?>

如何将此数组存储到mysql数据库中。

3 个答案:

答案 0 :(得分:1)

只需迭代数组并将每个元素插入表

foreach($hosts as $key => $value){
    mysql_query('INSERT INTO table (field) VALUES ("'.$value.'")');
}

答案 1 :(得分:1)

您可以使用PHP serialize函数在MySQL中存储数组和对象

答案 2 :(得分:1)

存储时可以使用json_encode:

$fruit_color = array("apple" => "red", "banana" => "yellow", "cherry" => "red");
$encoded = json_encode($fruit_color); //Turn your array into json-readable string

从数据库中检索时的json_decode:

$result = mysql_fetch_assoc($sql);
$decoded = json_decode( $result[column_name] );