I have a script that takes a variable number of inputs from a user and inserts them into a database table, with one row for each item. For small data sets, this works fine, but for larger data sets (more than 10 items) the script always fails with an out of memory error. Here's the code I'm using- is there a more memory efficient way of doing this?
$data = $_POST['dataArray'];
for ($i = 0; i < count($data); $i++)
{
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$stmt = $mysqli -> prepare('INSERT INTO items(name, serial, cost) VALUES (?, ?, ?)');
$stmt -> bind_param('sii', $data[$i]['name'], $data[$i]['serial'], $data[$i]['cost']);
$stmt -> execute();
$stmt -> close();
$mysqli -> close();
}
FWIW, the database table in question is just 4 columns:
- id [int(11), autoincrement]
- name [varchar(256]
- serial[int(11)]
- cost[int(11)];