Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i wrote a mysql query(procedure) for insert to user(userID,Role) and userID is auto increament. it works true. i want to get the userID with out parameters but i dont know how?!
my procedure query :

SQL
CREATE DEFINER=`root`@`localhost` PROCEDURE `Insert_User`(IN `Role` INT(1))
BEGIN
INSERT INTO user(UserID, Role)VALUES(null, Role);
END

i called it by php code:
if(isset($_POST['sbm']))
	{
		$pass = '1050585712';
		$role = $_POST['user'];
		switch($role)
		{
			case 0 :$role = 0;break;
			case 1 :$role = 1;break;
			case 2 :$role = 2;break;
			case 3 :$role = 3;break;
			case 4 :$role = 4;break;
			default:$role = 3;break;
		}
		if($con = mysqli_connect("localhost", "root", $pass))
		{
			mysqli_select_db($con, "toggery");
			mysqli_set_charset($con, "utf8");
			$p = $con -> prepare("CALL Insert_User(?)");
			$p -> bind_param('i', $role);
			$p -> execute();
		}
		else
		{
			echo 'db connection error!';
		}
	}


What I have tried:

how can i get UserID by php?
and how should i change the procedure query for do this?
Posted
Updated 1-Sep-16 2:01am
v3

1 solution

select LAST_INSERT_ID()

add that to the end of your procedure.

There could be a small chance of a race condition. I'm not too pro with mysql I'm afraid
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900