INSERT
通过查询在 PHP 代码中创建语句应该非常简单SELECT * FROM table
。这是一个例子:
$db = new PDO('mysql:dbname=DATABASE;host=HOST', 'USER', 'PASSWORD');
$result = $db->query('SELECT * FROM table');
$columns = array();
for($i = 0; $i < $result->columnCount(); $i++) {
$column = $result->getColumnMeta($i);
$columns[] = sprintf('`%s`', $column['name']);
}
$colstring = implode(',', $columns);
$rows = $result->fetchAll(PDO::FETCH_NUM);
$inserts = array();
foreach($rows as $r) {
$values = array_map(function($e) use ($db) {
return $db->quote($e);
},
$r);
$valstring = implode(',', $values);
$inserts[] = sprintf('INSERT INTO table (%s) VALUES (%s)',
$colstring,
$valstring);
}