gpt4 book ai didi

php - MySQLi insert_id()

转载 作者:可可西里 更新时间:2023-11-01 06:31:44 27 4
gpt4 key购买 nike

我不知道,我的代码有什么问题。我尝试获取最新的插入 ID,它正在回显 0。有什么想法吗?

public function __construct() {
$this->mysqli = new mysqli(MYSQLI_SERVER, MYSQLI_USER, MYSQLI_PWD, MYSQLI_DBNAME) or die ('Error with connecting to the database!');;
}
public function insert_id(){
return $this->mysqli->insert_id;
}

$db->query("INSERT INTO user(f_name, l_name) VALUES('$f_name', '$l_name')");
var_dump($db->insert_id()); // return 0?

最佳答案

您应该按如下方式使用它:

<?php
$mysqli = new mysqli(SQLI_SERVER, MYSQLI_USER, MYSQLI_PWD, MYSQLI_DBNAME);
if ($result = $mysqli->query("INSERT INTO user(f_name, l_name) VALUES('$f_name', '$l_name');")) {
echo 'The ID is: '.$mysqli->insert_id;
}
?>

关于php - MySQLi insert_id(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5844151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com