Using MySQLi add multiple records to MySQL
With the mysqli_multi_query()
function, you must run more than one SQL statement.
The \”student\” table gets three new rows from the following examples:
?php
$servername = \”localhost\”;
$username = \”username\”;
$password = \”password\”;
$dbname = \”school\”;// Create connection
$link = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$link) {
die(\”Connection failed: \” . mysqli_connect_error());
}$sql = \”INSERT INTO student (firstname, lastname, email)
VALUES (\’ram\’, \’lal\’, \’ramlal@example.com\’);\”;
$sql .= \”INSERT INTO student (firstname, lastname, email)
VALUES (\’shyam\’, \’lal\’, \’shyam@example.com\’);\”;
$sql .= \”INSERT INTO student (firstname, lastname, email)
VALUES (\’ajay\’, \’lal\’, \’ajay@example.com\’)\”;if (mysqli_multi_query($link, $sql)) {
echo \”New records created successfully\”;
} else {
echo \”Error: \” . $sql . \”<br>\” . mysqli_error($link);
}mysqli_close($link);
?>