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
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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.