[英]PHP MySql trying to get number of last row
I've got a table with two columns "Number" and "Date". 我有一个带有两列“ Number”和“ Date”的表。 I want retrieve the value of the last row however this isn't working:
我想检索最后一行的值,但是这不起作用:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Number FROM functions_order_numbers ORDER BY Number DESC LIMIT 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row["Number"];
}
mysqli_close($conn);
Table: Table Screenshot 表格: 表格截图
I'm trying to create an incremental unique order number 我正在尝试创建一个递增的唯一订单号
If you want an incremental number in the table, then use auto_increment
: 如果要在表中增加数字,请使用
auto_increment
:
create table t (
number int auto_increment primary key, -- usually it is a primary key
. . .
);
Don't attempt to do this at the application layer. 不要尝试在应用程序层执行此操作。 It is dangerous, because multiple updates at the same time might not do the right thing.
这是很危险的,因为同时进行多个更新可能不会做正确的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.