简体   繁体   English

根据日期选择最近的5行

[英]Select the most recent 5 rows based on date

I haven't touched PHP in a while and trying to select the 5 most recent entries in my database and print them to screen. 我有一段时间没有触及PHP,并尝试在我的数据库中选择5个最新的条目并将它们打印到屏幕上。

I see mysql command isn't recommended anymore and to use PDO->mysql instead. 我不推荐使用mysql命令,而是使用PDO-> mysql。

My query is something like this: 我的查询是这样的:

SELECT id,title,date,author FROM table ORDER BY date DESC LIMIT 5;

I'm assuming I would have to put the values into an array and create a loop and output the results. 我假设我必须将值放入数组并创建一个循环并输出结果。

<?php
$db = new PDO('mysql:dbhost='.$dbhost.';dbname='.$dbname, $user, $pass);

while () {
  print($title[$i], $date[$i], $author[$i]);
  $i++
}

$db = null;

?>

I'm stuck filling in the gaps with the above code. 我用上面的代码填补了空白。

Update: The $db = new PDO.... line is reporting an error message: 更新:$ db = new PDO ....行报告错误消息:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] Can't connect to local MySQL server through socket... in /var/...

PDO is confirmed to be installed and enabled. 确认已安装并启用PDO。 My other web apps on the server can connect to the same remote mysql server fine. 我在服务器上的其他Web应用程序可以很好地连接到同一个远程mysql服务器。

<?php
$host = 'localhost'; $db = 'db-name'; $user = 'db-user'; $pw = 'db-password';
$conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

<?php
$sql = "SELECT id,title,date,author FROM table ORDER BY date DESC LIMIT 5";
$query = $conn->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
$totalRows = $query->rowCount();
?>

<?php do {
// print your results here ex: next line
echo 'Title: '.$row['title'].' Date: '.$row['date'].' Author: '.$row['author'].'<br>'; 
} while ($row = $query->fetch(PDO::FETCH_ASSOC)); ?>

Happy Coding ! 快乐的编码!

Don't forget to close and release resources 不要忘记关闭并释放资源

<?php $query->closeCursor(); ?>

EDIT 编辑

I recommend not echoing error messages once you have confirmed your code functions as expected; 一旦您按预期确认了代码功能,我建议不要回显错误消息; however if you want to simply use plain text you can do this... 但是,如果你想简单地使用纯文本,你可以这样做......

You can add this to your connection block... 您可以将其添加到您的连接块...

if ($conn->connect_error) {
    die("Database Connection Failed");
    exit;
}

You can also change your query block... 您还可以更改查询块...

try {
    $sql = "SELECT id,title,date,author FROM table ORDER BY date DESC LIMIT 5";
    $query = $conn->prepare($sql);
    $query->execute();
    $row = $query->fetch(PDO::FETCH_ASSOC);
    $totalRows = $query->rowCount();
} catch (PDOException $e) {
    die("Could not get the data you requested");
    exit;
}

Again, it is recommended that errors not be echoed. 同样,建议不要回显错误。 Use error checking only for debugging. 将错误检查用于调试。

<?php
$db = PDO('mysql:dbhost=$dbhost;dbname=$dbname', $user, $pass);
$sth = $db->prepare("SELECT id,title,date,author FROM table ORDER BY date LIMIT 5");
$sth->execute();

$result = $sth->fetch(PDO::FETCH_ASSOC);
print_r($result);
?>

in a loop: 在循环中:

while($row = $sth->fetch()) {
    echo $row['column'];
}

From the documentation: http://php.net/manual/en/pdostatement.fetch.php 来自文档: http//php.net/manual/en/pdostatement.fetch.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM