加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在存储过程后获取行的id

发布时间:2020-12-13 17:01:20 所属栏目:PHP教程 来源:网络整理
导读:我刚刚执行了一个存储过程,它将航班信息插入到我的表中,我想在每次插入后获取id.但我无法找到其他适用于我的方法.我想得到auto_increment上的flight_id. mysqli_query($conn,"CALL addschedule('$airlineID','$from','$to','$departDate','$arriveDate','$de
我刚刚执行了一个存储过程,它将航班信息插入到我的表中,我想在每次插入后获取id.但我无法找到其他适用于我的方法.我想得到auto_increment上的flight_id.

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

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

运行mysqli_insert_id只返回值0,但我的最后一次航班ID为3.有人能给我一个明确的解释吗?

解决方法

mysqli_insert_id — Returns the auto generated id used in the last query

The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. 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.

因此,请检查您是否已将AUTO_INCREMENT属性设置为您的ID字段.

或者,如果您已将其设置为AUTO_INCREMENT.此示例代码可能对您有所帮助.

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


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

/* In op's case
mysqli->query($conn,'$price')");
*/

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


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

答案2

在你的SP

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

在你的PHP代码中

$result=mysqli_query($conn,'$price')");

echo "Last ID: ".$result;

这可能会使你的工作.

PS:我没有测试这段代码.希望工作

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读