[PHP]INSERT文を作る
INSERT文を作る
echo getInsertQueryByKey('system_table', array('name','gender'), array( array('jeff','male'), array('pola','female') ) ); function getInsertQueryByKey($tableName,$columns,$datas){ //SQLite古いバージョン $w=array(); $column = implode(',', $columns); $query="INSERT INTO {$tableName} ({$column}) SELECT "; $tmp=array(); foreach ($columns as $index => $columnName) { $tmp[] = "'". $datas[0][$index] . "'" . ' AS ' . $columnName; } $query .= implode(', ', $tmp); $w[]=$query; foreach ($datas as $index => $data) { if($index<1)continue; $tmp=array(); foreach ($columns as $index2 => $columnName) { $tmp[]=$data[$index2]; } $w[]="'".implode("', '", $tmp)."'"; } return implode(' UNION ALL SELECT ', $w).';'; /* MySQL or SQLite(バージョン 3.7.11以降) $queryValues=array(); foreach ($datas as $index => $value) { $values=array(); foreach ($value as $index2 => $value2) { $values[]="'".$value2."'"; } $queryValues[]='('.implode(',', $values).')'; } $column=implode(',', $columns); $value=implode(',', $queryValues); $sql="INSERT INTO {$tableName} ({$column}) VALUES {$value};"; return $sql; */ }