php - How do i get the id of a row after i stored procedure -
i executed stored procedure inserts flight information table , wanted id after every insert made. not able find other ways work me. wanted flight_id 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;
running mysqli_insert_id
returns me value of 0 last flight id 3. can give me clear explanation?
mysqli_insert_id — returns auto generated id used in last query
the mysqli_insert_id() function returns id generated query on table column having auto_increment attribute. if last query wasn't insert or update statement or if modified table not have column auto_increment attribute, function return zero.
so please check had set auto_increment attribute id
field.
or if had set auto_increment. sample code might you.
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); $query = "insert mycity values (null, 'stuttgart', 'deu', 'stuttgart', 617000)"; $mysqli->query($query); /* in op's 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(); ?>
answer 2
in sp
insert table values (null, 'stuttgart', 'deu', 'stuttgart', 617000); select last_insert_id();
in php code
$result=mysqli_query($conn,"call addschedule('$airlineid','$from','$to','$departdate','$arrivedate','$departtime','$arrivaltime','$price')"); echo "last id: ".$result;
this might work.
ps: didn't test code. hope work
Comments
Post a Comment