根据日期选择最近的5行

时间:2022-01-09 12:21:37

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 ....line正在报告一条错误消息:

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. My other web apps on the server can connect to the same remote mysql server fine.

PDO已被确认安装并启用。我服务器上的其他web应用程序可以很好地连接到同一个远程mysql服务器。

2 个解决方案

#1


4  

<?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.

再次建议不要重复错误。仅对调试使用错误检查。

#2


-1  

<?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

#1


4  

<?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.

再次建议不要重复错误。仅对调试使用错误检查。

#2


-1  

<?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