[英]How to output a value that was recently added into mysql database?
I want to create a table that will output the data from mysql database that was recently added. 我想创建一个表,该表将从最近添加的mysql数据库中输出数据。
Example : 范例:
+-----+---------+-----------+---------------+-------------+
| id | item_id | item_name | borrowed_date | expiry_date |
+-----+---------+-----------+---------------+-------------+
| B01 | N01 | book | 12/05/2017 | 10/06/2017 |
+-----+---------+-----------+---------------+-------------+
I have tried using ORDER BY but it does not output according to newly added row. 我尝试使用ORDER BY,但是它不会根据新添加的行输出。
<?php
include"connection.php"; //contain $conn
$query = "SELECT * FROM `database` ORDER BY item_id ASC ;";
$result = mysqli_query($conn,$query);
?>
The output was not according to newly added data 输出不是根据新添加的数据
Set your id column to auto increment Give your table another name than Database. 将您的id列设置为自动递增为表指定一个不同于数据库的名称。
Then use: 然后使用:
SELECT * FROM `yourtable` ORDER BY `id` DESC LIMIT 0,1;
I suggest including one column (like a primary key) that's set to AUTO_INCREMENT . 我建议包括设置为AUTO_INCREMENT的一列(如主键)。 That way you can sort in descending order (DESC) by that ID to get the latest entry.
这样,您可以按该ID降序(DESC)排序以获得最新条目。
Something like this: 像这样:
CREATE TABLE `database` (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
[other columns here]
PRIMARY KEY (id)
);
And then: 接着:
SELECT * FROM `database` ORDER BY `id` DESC LIMIT 0,1;
Incidentally, I'm not sure if your table is really named "database". 顺便说一句,我不确定您的表是否真的命名为“数据库”。 If so, I suggest not using a reserved word .
如果是这样,我建议不要使用保留字 。 It will work since you're using backticks, but it might be a good idea to change it anyway for various reasons .
由于您正在使用反引号,因此它会起作用,但是由于各种原因 ,无论如何都要更改它可能是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.