簡體   English   中英

如果在db查詢中沒有返回結果,如何顯示消息

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

想知道是否有人可以提供幫助。 我正在運行有關php和mysql的教程,其中有一個笑話數據庫,其中包含:

  • 笑話桌上的笑話
  • 作者表中的作者
  • 類別表中的類別

我有一個名為authors.html.php的頁面,其中包含作者列表,並提供添加作者,編輯/刪除作者的選項(每個返回作者的名字旁邊)。 我想做的是,如果包含作者的數據庫為空,而不顯示以下php錯誤:

注意:未定義的變量:第22行的C:\\ wamp \\ www \\ chapter7 \\ admin \\ authors \\ authors.html.php中的作者

我想顯示一個整潔的消息,說沒有找到結果。

我的代碼如下:

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

請注意,我對此非常陌生,並且已經嘗試了一些失敗了的迭代,因此,非常感謝您的任何幫助,甚至只是簡單的指導,這些指導都會使我逐步思考如何創建這種功能。 不僅要尋找正確的答案,還想了解如果有人可以幫助的話,那將是很棒的。

與此類似的東西應該起作用。 我過去使用過它,對我有用。 您將需要針對數據庫和表調整查詢等,但這應該沒問題。

$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"];
        }
    }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM