How to use mysqli->multi_query()?

Kayes picture Kayes · Dec 26, 2010 · Viewed 9.7k times · Source

Can anyone show me how to use mysqli->multi_query() to execute multiple update/ insert queries on a single connection? Thanks.

I did follow the tutorials in the PHP manual. But I have some issues.

My batch of queries has 5 statements separated by semicolons.

UPDATE scenes set UserID = '11111111' WHERE ID = '031DFDAD92F6F4AB64AF317C06D64089DF119EC2';

INSERT INTO surfaces (ID, Name, SceneID, SurfaceTypeID, Color) VALUES('5F9A7301C2D398C4D1B90BA5AA56A9DED3FAA639', 'front ', '031DFDAD92F6F4AB64AF317C06D64089DF119EC2', 1, 11432044);

INSERT INTO regions (ID, SurfaceID, x, y, width, height, RegionMovieClip) VALUES('864406A2CB30CFBE846ED7B0B08A79BD5605037D', '5F9A7301C2D398C4D1B90BA5AA56A9DED3FAA639', 375, 22, 104, 125, 'asdasdcvxcv');

INSERT INTO surfaces (ID, Name, SceneID, SurfaceTypeID, Color) VALUES('1FCA2131ED1B89206E4E66DBE20D8D09513FF39D', 'floor ', '031DFDAD92F6F4AB64AF317C06D64089DF119EC2', 1, 7318465);

INSERT INTO regions (ID, SurfaceID, x, y, width, height, RegionMovieClip) VALUES('DBD0E85EAEE2685E2AEC590C8CA214C3C5653971', '1FCA2131ED1B89206E4E66DBE20D8D09513FF39D', 272, 288, 114, 89, 'asdasd')

All are executed except for the 3rd query. That's where I'm lost. If there's an sql error inserting the 3rd query, how can I get it? And how the queries after the failed 3rd one is getting executed?

Answer

evan picture evan · Dec 26, 2010

The examples don't really show how to handle an error.

Execution will stop after the first failure.

Use mysqli_error() to get error information.

mysqli_use_result() returns false if an error occured - if you count the results and there aren't enough, an error occurred.