Jump to content

mysqli_multi_query() function


CORT0619

Recommended Posts

I have googled examples of the use of the mysqli_multi_query and I can't really find anything that applies to what I am doing. Essentially I want to do a insert query, a select query and then a insert query that are dependent on one another but I can't find any examples of anything like this. Does anyone know how to use the mysqli_multi_query() function well and can explain how I can use this??

 

Thanks alot!

Link to comment
https://forums.phpfreaks.com/topic/245323-mysqli_multi_query-function/
Share on other sites

i did something like the following and i'm receiving a syntax error:

 

$q = "INSERT INTO blah(username, password) VALUES('$username', '$password', 1)";

$q .= "SELECT user_id FROM blah WHERE login_name='$login_name'";

$q .= "INSERT INTO blah2(first_name, last_name, user_id) VALUES('$first_name', '$last_name', '$user_id')";

$r = mysqli_multi_query($db, $q);

if($r) {

 

but like i said i'm receiving a syntax error. Any ideas?

your queries need semi colons at the end of them, notice my code. Also your first query has more values than columns

 

$q = "INSERT INTO blah(username, password) VALUES('$username', '$password', 1);";

$q .= "SELECT user_id FROM blah WHERE login_name='$login_name';";

$q .= "INSERT INTO blah2(first_name, last_name, user_id) VALUES('$first_name', '$last_name', '$user_id');";

$r = mysqli_multi_query($db, $q);

Thanks alot that fixed the syntax error that I was receiving but the only problem is the second insert statement is not working and the reason I did the select statement was to get the user_id because the user_id is a foreign key so it has to be the same in both tables.

Save it to a mysql variable like this:

 

$q = "INSERT INTO blah(username, password) VALUES('$username', '$password', 1);";

$q .= "set @user_id = (SELECT user_id FROM blah WHERE login_name='$login_name' limit 1);";

$q .= "INSERT INTO blah2(first_name, last_name, user_id) VALUES('$first_name', '$last_name', @user_id);";

$r = mysqli_multi_query($db, $q);

Guy,

 

You are amazing!! I set the select to a variable just like you said and it worked perfectly. I understand mysql okay, but adding php to it is different because you have to figure out how to use certain functions with php. Is there somewhere I can go to find out how to use mysql in php?

 

Also with the insert last id query, where would I use that? Where I manage my database or in my php?

 

Once again thanks so much for your help I've been struggling with this problem for a while.

You would/could use it like this:

$q = "INSERT INTO blah(username, password) VALUES('$username', '$password', 1);";

$q .= "INSERT INTO blah2(first_name, last_name, user_id) VALUES('$first_name', '$last_name', last_insert_id());";

$r = mysqli_multi_query($db, $q);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.