I couldn’t get the insert stored procedure to work with auto increment in php from mysql.
<?php
//connect to database
$connection = mysqli_connect("localhost", "user", "password", "buildings", "3306");
//Having problems with inserting stored procedure because
// the mysql_insert_id()) would not work because it is auto increment
//calling local variables
$Firstname = "jamie";
$Lastname = "jones";
$Password = "1234";
$EmailAddress = "jones@gmail.com";
$Salt = "23";
$RoleID = 1;
//calling stored procedure called registerusers that pass all of parmeters
$sql = mysqli_query($connection, "CALL registerusers(NULL,'$Firstname,$Lastname,$Password,$EmailAddress,$Salt,$RoleID')");
//suppose to print out the id number but it didn't work at all
printf("Last inserted record has id %d\n", mysqli_insert_id());
?>
That suggests an error in the way you call the stored procedure, or something in the procedure itself. In the first code you posted, you seem to have surrounded all the parameters in single quotes, which would cause them to be passed as a single string. Have you changed that now? Can you post the current code exactly as it is?
" If the last query wasn’t an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero. "
Your last query in the call was “SELECT @new_id”, so the insert_id returns zero. Looking at the stored procedure, you should retrieve the value from that SELECT to get the insert id.
Yes, but if you read the documentation, you won’t get a value in mysqli_insert_id because of that final select. From what I read, and I don’t know stored procedures so it’s only based on the doc, but you need to do something like: