我需要將數組數據插入到MySQL DB中。我的代碼在下面提供。問題是,query
等於將多維數組數據插入到MySQL DB中
INSERT INTO
MyTab
(Array) VALUES (Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array)
那麼,爲什麼會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)";
UPDATE 這段代碼在該行$vals = implode(...)
返回內部服務器錯誤。
$ columns = array('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 . "')";
因爲他們是數組? – Ryan
嘗試刪除$列後的括號 – Peter