我想从表中获取MAX ID并为其添加一个数字。在执行查询后存储结果时,我将其保存到另一个变量中并添加1。我收到一个错误:“ mysqli_result类的对象无法转换为int”
我尝试过:
$row = mysqli_fetch_assoc($result);
$uniqueCode = $row+1;
这是我必须获取最大参考号的当前代码:
$sql = "SELECT MAX(referenceNo) FROM taxibookings LIMIT 1";
$result = mysqli_query($conn, $sql);
答案 0 :(得分:1)
如果您正在使用自动增量,并且想要查找最大id,那么很简单,如果不删除行,则查找表的最后一个条目,因为如果要删除行,那么它永远不会处理数据库的坏方法如果需要删除,请使用“状态”列对其进行删除。请参见以下代码。
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// find last id which will be the max
if ($conn->query($sql) === TRUE) {
$last_id = $conn->insert_id;
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
答案 1 :(得分:0)
尝试此代码
$sql = "SELECT MAX(referenceNo) as referenceNo FROM taxibookings";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_assoc($result);
$uniqueCode = $data['referenceNo']+1;