简体   繁体   中英

How to Show message if no results returned from db query in php

Wondering if anyone can help at all. I am running through a tutorial on php and mysql, where there is a joke database which holds:

  • Jokes in joke table
  • Authors in author table
  • Categories in category table

I have a page called authors.html.php that contains a list of authors with options to add author, edit/delete author (next to name of each of returned authors). What i am trying to do is if the db containing the authors is empty instead of displaying the following php error:

Notice: Undefined variable: authors in C:\\wamp\\www\\chapter7\\admin\\authors\\authors.html.php on line 22

I want to display a nice tidy message saying no results found.

My code is as follows:

<?php   
  include $_SERVER['DOCUMENT_ROOT'] . '/chapter7/includes/db.inc.php';
    try {
            $result = $pdo->query('SELECT id, name FROM author');
    } catch (PDOException $e) {
            $error = 'Error fetching authors from the database!';
            include $_SERVER['DOCUMENT_ROOT'].
                            '/chapter7/admin/error.html.php';
            exit();
    }       

    foreach ($result as $row){
            $authors[] = array('id' => $row['id'], 'name' => $row['name']);
        }

    include 'authors.html.php';

    if (isset($_POST['action']) and $_POST['action'] == 'Delete') {
            include $_SERVER['DOCUMENT_ROOT'].
                            '/chapter7/includes/db.inc.php';
            //Get jokes belonging to author 
            try {
                $sql = 'SELECT id FROM joke WHERE authorid = :id';
                $s = $pdo->prepare($sql);
                $s->bindValue(':id', $_POST['id']);
                $s->execute();
            } catch (PDOException $e) {
                $error ='Error getting list of jokes to delete.';
                include $_SERVER['DOCUMENT_ROOT'].
                                '/chapter7/admin/error.html.php';
                exit();
            }
            $result = $s->fetchAll();
            //Delete joke category entries
            try {
                $sql = 'DELETE FROM jokecategory WHERE jokeid =:id';
                $s = $pdo->prepare($sql);
                //For each joke
                foreach ($result as $row) {
                        $jokeId= $row['id'];
                        $s->bindValue(':id', $jokeId);
                        $s->execute();      
                }
            } catch (PDOException $e) {
                $error = 'Error deleting category entries for joke.';
                include $_SERVER['DOCUMENT_ROOT'].
                                '/chapter7/admin/error.html.php';
                exit();
            }
            //Delete jokes belonging to author
            try {
                $sql = 'DELETE FROM joke WHERE authorid = :id';
                $s = $pdo->prepare($sql);
                $s->bindValue(':id', $_POST['id']);
                $s->execute(); 
            } catch (PDOException $e) {
                $error = 'Error deleting jokes for author';
                include $_SERVER['DOCUMENT_ROOT'].
                                '/chapter7/admin/error.html.php';
                exit();
            }
            //Delete the author
            try {
                $sql = 'DELETE FROM author WHERE id =:id';
                $s = $pdo->prepare($sql);
                $s->bindValue(':id', $_POST['id']);
                $s->execute();
            } catch (PDOException $e) {
                $error ='Error deleting author';
                include $_SERVER['DOCUMENT_ROOT'].
                          '/chapter7/admin/error.html.php';
                exit();
            }
            header('Location: .');
            exit();
    }       

please be aware i am extremely new to this and have already attempted some iterations that failed miserably so any assistance or even just simple guidance that would step me through the thinking around creating such functionality would be greatly appreciated. Not just looking for the right answer but want to understand the thinking behind if anyone can help that would be great.

Something similar to this should work. I have used this in the past and it has worked for me. You will need to adjust the query and such to your DB and tables, but it should be fine.

$stm = $PdoObj->prepare("SELECT * FROM NEWS_articles");
    $stm ->execute();
    $results = $stm ->fetchAll();

    if($results==null){
        echo "<p>No dice, try other criteria</p>";
    }else{
        foreach($results as $row){
            echo $row["articleName"];
        }
    }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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