如何将php数组数据存储到mysql数据库中
问题描述:
<?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数据库中。如何将php数组数据存储到mysql数据库中
答
只是遍历数组,每一个元素插入到表
foreach($hosts as $key => $value){
mysql_query('INSERT INTO table (field) VALUES ("'.$value.'")');
}
答
您可以使用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]);
你应该从这里学习基础开始:http://www.w3schools.com/php/php_mysql_intro.asp – 2010-11-27 11:00:39