How to get row id after stored procedure

I just performed a stored procedure that inserts flight information into my table, and I wanted to get an identifier after every insert I made. But I could not find other ways that could work for me. I wanted to get flight_id, which is on auto_increment.

mysqli_query($conn,"CALL addschedule('$airlineID','$from','$to','$departDate','$arriveDate','$departTime','$arrivalTime','$price')");

$last_id = mysqli_insert_id($conn);
echo $last_id;

The launch mysqli_insert_idonly returns 0, but my last flight identifier was 3. Can someone give me a clear explanation?

+4
source share
1 answer

mysqli_insert_id - returns the automatically generated identifier used in the last query

mysqli_insert_id() , , AUTO_INCREMENT. INSERT UPDATE, AUTO_INCREMENT, .

, , AUTO_INCREMENT id.

AUTO_INCREMENT. .

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");


$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);

/* In op case
mysqli->query($conn,"CALL addschedule('$airlineID','$from','$to','$departDate','$arriveDate','$departTime','$arrivalTime','$price')");
*/

printf ("New Record has id %d.\n", $mysqli->insert_id);


/* close connection */
$mysqli->close();
?>

2

SP

INSERT INTO table VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000);
select LAST_INSERT_ID();

php-

$result=mysqli_query($conn,"CALL addschedule('$airlineID','$from','$to','$departDate','$arriveDate','$departTime','$arrivalTime','$price')");

echo "Last ID: ".$result;

.

PS: .

+3

Source: https://habr.com/ru/post/1652795/


All Articles