How to get LAST_INSERT_ID () from a mysqli_multi_query () function with SET and INSERT?

0

This is my 'method' of Placing Bets on my betting system.

In this method, I register data in two tables, a BET call and another BET_MATCHES, the system is controlled by time, so in all the registers I need to set the TIME_ZONE = '-03: 00' to make sure it does not there will be no error in the time, and after that execute the query that registers the header and generates the bet ID.

But when I retrieve the last id generated in this multi_query, the code returns me = 0 ... what can I do?

public function setBet($punter_name, $punter_phone, $value, $colab_id, $array_matches)
{
    // Post Header and Catch Bet ID
    $query =    "SET TIME_ZONE = '-03:00';";
    $query .= "INSERT INTO bet SET id ='NULL',
        punter_name ='$punter_name',
        punter_phone ='$punter_phone',
        value ='$value',
        colab_id ='$colab_id',
        date_time = now()";
    $data = mysqli_multi_query(Conexao::conectar(), $query) or die(Conexao::conectar());
    // For if data True, registered Header
    if ($data){
        // Get the id what we want
        $query = "SELECT LAST_INSERT_ID() FROM bet";
        $data = mysqli_query(Conexao::conectar(), $query);
        $line = mysqli_fetch_assoc($data);
        var_dump($line);
    }
    else
        echo "There was an error when registering the bet header";  
}
    
asked by anonymous 24.05.2016 / 21:06

1 answer

0

So the solution I found was based on the response of the @Miguel and @lpacheco

See the code

...
$conn = Conexao::conectar(); 
$query = "SET TIME_ZONE = '-03:00';";
$conn->query($query) or die('Erro na definição da timezone, tente mais tarde');
$query = "INSERT INTO bet (id, punter_name, punter_phone, value, colab_id, date_time, possible_prize) values(NULL, '$punter_name', '$punter_phone', '$value', '$colab_id', now(), 0);";
$data = $conn->query($query) or die('Erro na inserção, tente mais tarde');
$lastId = $conn->insert_id; // aqui guarda este e só este id, desta conecção que inseriu o novo dado

$query = "SELECT * FROM bet WHERE id=" .$lastId;
$data = mysqli_query($conn, $query);
...

so you can get the id of the last insert.

    
22.06.2016 / 16:15