Home > Enterprise >  ASC with MySQL Not Working but DESC Works
ASC with MySQL Not Working but DESC Works

Time:09-01

DESC is working but ASC isn't. Here is my code.

<?php
declare(strict_types = 1);                               // Use strict types
require 'includes/database-connection.php';              // Create PDO object
require 'includes/functions.php';                        // Include functions

$sql = "SELECT a.id, a.title, a.summary, a.category_id, a.member_id, 
               c.name AS category,
               CONCAT(m.forename, ' ', m.surname) AS author,
               i.file     AS image_file,
               i.alt      AS image_alt 
          FROM article    AS a
          JOIN category   AS c ON a.category_id = c.id
          JOIN member     AS m ON a.member_id   = m.id
          LEFT JOIN image AS i ON a.image_id    = i.id
         WHERE a.published = 1
      ORDER BY a.id ASC
         LIMIT 10;";                                      // SQL to get latest articles
$articles = pdo($pdo, $sql)->fetchAll();                 // Get summaries

$sql = "SELECT id, name FROM category WHERE navigation = 1;"; // SQL to get categories
$navigation  = pdo($pdo, $sql)->fetchAll();              // Get navigation categories

$section     = '';                                       // Current category
$title       = 'Creative Folk';                          // HTML <title> content
$description = 'A collective of creatives for hire';     // Meta description content
?>
<?php include 'includes/header.php'; ?>
  <main  id="content">
    <?php foreach ($articles as $article) { ?>
      <article >
        <a href="article.php?id=<?= $article['id'] ?>">
          <img src="uploads/<?= html_escape($article['image_file'] ?? 'blank.png') ?>"
               alt="<?= html_escape($article['image_alt']) ?>">
          <h2><?= html_escape($article['title']) ?></h2>
          <p><?= html_escape($article['summary']) ?></p>
        </a>
        <p >
          Posted in <a href="category.php?id=<?= $article['category_id'] ?>">
          <?= html_escape($article['category']) ?></a>
          by <a href="member.php?id=<?= $article['member_id'] ?>">
          <?= html_escape($article['author']) ?></a>
        </p>
      </article>
    <?php } ?>
  </main>
<?php include 'includes/footer.php'; ?>

index.php shows only 4 articles instead of 10 which is what I want. DESC works right. It shows 10 articles descending by id. I love PHP and MySQL. You know, when you need to do use the system commands or ftp to a server, PHP does it all!

Please help.

Thanks.

Neo

CodePudding user response:

You don't need insert ASC after a.id because the ORDER BY keyword sorts the records in ascending order by default.

$sql = "SELECT a.id, a.title, a.summary, a.category_id, a.member_id, 
           c.name AS category,
           CONCAT(m.forename, ' ', m.surname) AS author,
           i.file     AS image_file,
           i.alt      AS image_alt 
      FROM article    AS a
      JOIN category   AS c ON a.category_id = c.id
      JOIN member     AS m ON a.member_id   = m.id
      LEFT JOIN image AS i ON a.image_id    = i.id
     WHERE a.published = 1
  ORDER BY a.id
     LIMIT 10;";

CodePudding user response:

Fixed the problem. It was because of a NULL value in the 5th article.

  • Related