In PHP, it's not recommended to execute multiple INSERT statements in one query using the mysql_query function. The sample code provided in the question:
$string1= "INSERT INTO....;"; $string1 .= "INSERT INTO....;"; $string1 .= "INSERT INTO....;"; mysql_query($string1) or die(mysql_error());
is not an optimal approach for inserting multiple records into MySQL tables.
Instead, it's preferable to use a single INSERT query that inserts multiple values in one go. This method is more efficient and concise:
$query = "INSERT INTO table_name (column1, column2) VALUES (1, 2), (3, 4), (5, 6);"; mysql_query($query) or die(mysql_error());
By grouping the values for each column in parentheses and separating them with commas, you can insert multiple rows with a single query.
Using a single query to insert multiple records offers several advantages:
The above is the detailed content of Is Using Multiple INSERT Statements in a Single PHP Query Efficient?. For more information, please follow other related articles on the PHP Chinese website!