我需要将数组数据插入MySQL数据库。下面提供了我的代码。问题是query
等于
插入
MyTab
(数组)值(阵列,阵列,阵列
那么,为什么我要得到Array
而不是数组值呢?
$columns = array();
$values = array();
$columns[] = array('Num','appearanceTime');
$curr_time = new DateTime();
while($row=mysql_fetch_assoc($result_arr)) {
$values[] = array($row['Num_arr'],$curr_time);
}
$cols = implode(",",$columns);
$vals = implode(",",$values);
$query = "INSERT INTO `MyTab` ($cols) VALUES ($vals)";
更新此代码在$vals = implode(...)
行返回"内部服务器错误"。
$columns=数组('Num','appearanceTime','earliestTime');$values=array();
$curr_time = new DateTime();
while($row=mysql_fetch_assoc($result_arr)) {
$values[] = array($row['Num_arr'],$curr_time,$row['ETA']);
}
$cols = implode(",",$columns);
function get_values($arr) {
return '(' . implode(',', $arr) . ')';
}
$vals = implode(',', array_map('get_values', $values));
$query_queue = "INSERT INTO `MyTab` ('" . $cols . "') VALUES ('" . $vals . "')";
数组中的值是数组。你也需要implode
每个:
$vals = implode(',', array_map(function($arr) {
return '(' . implode(',', $arr) . ')';
}, $values));
至于专栏,我想你想要:
$columns = array('Num','appearanceTime');
$values = array();
不是:
$columns = array();
$values = array();
$columns[] = array('Num','appearanceTime');
您还需要引用所有内容才能将其放入查询中。如果可以的话,您应该使用PDO或MySQLi和prepared语句,而不是mysql_
。
给定PHP5.2,第一个示例需要更改为:
function implode_comma($arr) {
return '(' . implode(',', $arr) . ')';
}
# ...
$vals = implode(',', array_map('implode_comma', $values));