Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组元素插入数据库_Php - Fatal编程技术网

Php 将数组元素插入数据库

Php 将数组元素插入数据库,php,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 i

我想将每个数组的每个元素存储到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;

?>
您可以序列化数组并将序列化后的数组插入数据库。然后,我们将数据从数据库中取出,运行unserialize,它将返回到PHP数组中


更多信息:

如果您已经知道数组的键值对和MySQL表的列之间的映射,那么可以尝试使用数组构建MySQL语句:

$sql = "insert into $table ";
foreach($arr as $key => $value) {
    $sql .= "set $key = $value, ";
}
$sql .= ";";
如果您有一个数组结构,如array1,2,3,array4,5,6,array7,8,9

您知道,对于$arr as$key=>$val,您将得到类似于[1]=>array1,2,3的结果


虽然我不认为您能够使用该方法保存任意深度的数组。如果您事先不知道数组结构的深度,或它与数据库的关系,那么您可能应该保存一个序列化版本,如Mriigo所说。

但我的数组是数组中的数组,如..host=array1,2,3,array4,5,6,array7,8,9;现在我想将每个元素存储到mysql数据库中的不同字段中,比如1是一个字段,2是另一个字段,3是另一个字段,以此类推。你能给我itI的代码吗?我不知道如何在数组的键值对和mysql的表列之间进行映射mririgo 10分钟前,但我的数组是数组中的数组,如..host=array1,2,3,array4,5,6,array7,8,9;现在我想将每个元素存储到mysql数据库中的不同字段中,比如1是一个字段,2是另一个字段,3是另一个字段,以此类推。你能给我密码吗?