$sql = $db->build_insert(5, 2);
INSERT INTO `?` (
`?`,
`?`,
`?`,
`?`,
`?`
)
VALUES
(?, ?, ?, ?, ?),
(?, ?, ?, ?, ?);
| public function build_insert($fields_count, $values_count) | |
| { | |
| $indent = str_repeat(' ', 2); | |
| $ph = '?'; | |
| $ph_tick = "`{$ph}`"; | |
| $ph_table = $ph_tick; | |
| $ph_field = $ph_tick; | |
| $ph_fields = implode(",\n{$indent}", array_fill(0, $fields_count, $ph_field)); | |
| $ph_value = sprintf('(%s)', implode(', ', array_fill(0, $fields_count, $ph))); | |
| $ph_values = implode(",\n{$indent}", array_fill(0, $values_count, $ph_value)); | |
| $format_insert = "INSERT INTO %s (\n{$indent}%s\n)\nVALUES\n{$indent}%s;"; | |
| $sql = sprintf($format_insert, $ph_table, $ph_fields, $ph_values); | |
| return $sql; | |
| } |