Multiple querys with php

Asked

Viewed 35 times

0

hello everyone would like to know how to run two querys at the same time in php the situation and the following intend to do an Insert and return the auto_increment key I am doing the following

$query = "INSERT INTO usuario(cd_usuario,nome_usuario)VALUES ('0000','Fabricio')ON DUPLICATE KEY UPDATE nome_usuario = 'Fabricio Silva';SELECT LAST_INSERT_ID() AS ID";
$query = mysqli_multi_query($conexao,$query);//Ate aqui funciona
$query = mysqli_store_result($query);
$query = mysqli_fetch_array($query);

In this case you will not activate ON DUPLI.... because I am having new key generated, I need to recover the user id to assign an image to it

  • You need to call the mysqli_next_result() first.

  • could exemplify and explain for what purpose ?

  • 2

    It’s no longer simple to use mysqli_insert_id to get the entered record id?

  • did not know the command vo a look

  • 1

    uaaau worked perfectly with the very simple code the last doubt and if you do not run the risk of it picking up an id from another section or at the time I pick up the id of the bank could be from another register

  • 1

    You don’t have that problem.

  • This is called "race condition", but Mysql will get the last id of the current connection and not the last one of the database when calling the function, so there is no problem. Well, actually there is a specific condition for this to occur, which is not your case. This occurs only if using replicates (Master - Master in "Active-Active Mode"), collision of AUTO_INCREMENT between banks, but this is a Mysql problem, which is "predictable" in this type of replica. In this case you will have to use the auto_increment_increment and the auto_increment_offset.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.