Sometimes you need to insert several records into the database at the same time. Instead of running mysqli_query() over and over, you can concatenate your SQL statements and run them all at once!
To insert multiple records, you can string your INSERT INTO queries together, separated by semicolons (;).
Then, instead of using mysqli_query(), you use the mysqli_multi_query() function.
<?php $servername = "localhost"; $username = "root"; $password = "your_password"; $dbname = "myDB";$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
// String multiple queries together separated by a semicolon $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com');"; $sql .= "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', 'mary@example.com');"; $sql .= "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Julie', 'Dooley', 'julie@example.com');";
// Use multi_query to execute them all! if (mysqli_multi_query($conn, $sql)) { echo "New records created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
mysqli_close($conn); ?>
Which character is used to separate multiple SQL statements when stringing them together for execution?