user1052448
user1052448

Reputation: 433

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.

I see mysql command isn't recommended anymore and to use PDO->mysql instead.

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:

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.

Upvotes: 6

Views: 2041

Answers (2)

Kuya
Kuya

Reputation: 7310

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

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.

Upvotes: 4

Marcin
Marcin

Reputation: 1494

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

Upvotes: -1

Related Questions