【发布时间】:2018-03-26 06:24:35
【问题描述】:
我想使用以下函数返回最后插入的 id: MySql 存储函数:
CREATE DEFINER=`root`@`localhost` FUNCTION `set_user`( user_id int(11), u_name varchar(50),
pass varchar(128)) RETURNS int(11)
BEGIN
DECLARE newest_id INT(11);
INSERT INTO `test_db`.`users`
(`id`,`user_name`,`password`)
VALUES (user_id, u_name, pass);
SELECT LAST_INSERT_ID() INTO newest_id FROM test_db.users;
RETURN newest_id;
END
这里是php代码:
$sp = "select test_db.set_user(1, 'uname', 'pass123')";
$res = $con->multi_query( $sp );
if( $res ) {
$results = 0;
if ($result = $con->store_result()) {
printf( "<b>Result #%u</b>:<br/>", ++$results );
while( $row = $result->fetch_assoc() ) {
echo $row["newest_id"];
}
$result->close();
}
}
函数成功插入数据但是php没有得到单一的返回值。请帮忙。
【问题讨论】:
-
回显 mysqli_insert_id($con);
-
不通过存储过程,只需使用
mysqli_insert_id($con)获取最后插入的id。非常简单的原生功能 -
你还说:-
the function is successfully inserts the data-> 哪个函数?我在你的代码中看不到任何函数。2.我也看不到任何数据插入代码。 -
@AlivetoDie 这个函数 test_db.set_user(1, 'uname', 'pass123') 谢谢
-
你为什么使用
multi_query()进行单个查询?