Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute multiple insert, update, queries separated by ";" at once!

Tags:

php

mysql

pdo

I use remote server to store values into MySQL database.

I don't use any SELECT queries, only INSERT, DELETE and UPDATE.

When I execute 70,000 queries I have trouble with speed.

I was trying mysql_unbuffered_query and I saw little performance profit but it's still too slow.

I remember that some functions in PHP allow me to execute several lines, separated by ;

$something=<<<something

query1 ;
query2 ;
query3 ;

something;

execute($something)

Can someone can help me remember ?

Maybe in PDO ?

like image 524
marc Avatar asked Dec 06 '25 12:12

marc


1 Answers

The feature you are thinking of is the PHP heredoc syntax. It allows you to do multiple line strings using <<<.

But PDO does not support multiple SQL statements at once. You need to split them up and execute each one individually. You said this was too slow for you, so you may not be able to use PDO.

If you have many statements that are the same but with different parameters, you can use PDO prepared statements. With these, you can prepare the statement once, then just execute it with different parameters. If you have multiples types of statements, you will need to prepare each individually. You can read more about prepared statements here. They also have the benefit of not being vulnerable to SQL injection.

like image 70
Alan Geleynse Avatar answered Dec 08 '25 02:12

Alan Geleynse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!