[英]10 most recent rows from mySQL by time and define variables
I want to get the 10 most recent rows from a mysql database. 我想从mysql数据库中获取最近的10行。
How can I do that? 我怎样才能做到这一点?
+----+-------+----+-----------+
| id | value | ap | timestamp |
+----+-------+----+-----------+
And define variables for value, ap, and timestamp... for each row! 并为每行定义值,ap和时间戳的变量!
like: 喜欢:
$value1 = ?;
$ap1 = ?;
$timestamp1 = ?;
$value2 = ?;
$ap2 = ?;
$timestamp2 = ?;
etc....
The simplest way is probably something like this: 最简单的方法可能是这样的:
$db = new PDO('mysql:host=<hostname>;port=<port>;dbname=<database name>', '<username>', '<password>');
$query = 'SELECT id, value, ap, `timestamp` '.
'FROM <table> '.
'ORDER BY `timestamp` DESC '.
'LIMIT 10';
$resultset = $db->query($query);
$results = $resultset->fetchAll();
Then you will have a two-dimensional array with 10 sets of the values you want. 然后,您将拥有一个包含10组所需值的二维数组。
$results[0]['id']
, $results[0]['value']
, $results[0]['ap']
, and $results[0]['timestamp']
, up through $results[9]['id']
, etc. $results[0]['id']
, $results[0]['value']
, $results[0]['ap']
和$results[0]['timestamp']
,直到$results[9]['id']
等
You can get the data like so.. 您可以像这样获取数据。
$query = mysql_query("SELECT * FROM `my_table` ORDER BY `timestamp` DESC LIMIT 10");
and you can use the results like so.. 您可以像这样使用结果。
while($results = mysql_fetch_object($query)) {
echo $results->value;
echo $results->ap;
echo $results->timestamp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.