PHP - 无法输出表中没有数据的数据

时间:2017-05-12 12:32:39

标签: php pdo

我已经尝试查找显示数据的任何引用,其中表格中没有数据一小时,但我还没有找到解决问题的代码

我有这段代码:

//function

function readAll(){
    $query = "SELECT * FROM ".$this->table_name." ORDER BY id_nilai ASC";
    $stmt = $this->conn->prepare( $query );
    $stmt->execute();
    return $stmt;
}

//execute
$pro3 = new Nilai($db);
$stmt3 = $pro3->readAll();
while ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)){
            // not yet fix how to display no data
            //if($row3==false){  tried to change $row3==0 still won't work
                //echo "No Data";
            //}
              //die(var_dump($row3)); showing `bool(false)`
            echo $row3['ket_nilai'] (echo $row3['jum_nilai'])
        }

任何想法怎么做?

1 个答案:

答案 0 :(得分:0)

  

我想在数据库表中找不到数据时显示一些文本

你需要在进入循环之前获取结果,检查你是否得到了结果然后如果你进入循环,否则显示消息

见下面的代码:

<?php

//function

function readAll(){
    $query = "SELECT * FROM ".$this->table_name." ORDER BY id_nilai ASC";
    $stmt = $this->conn->prepare( $query );
    $stmt->execute();
    return $stmt;
}

//execute
$pro3 = new Nilai($db);
$stmt3 = $pro3->readAll();
$results = $stmt3->fetchall(PDO::FETCH_ASSOC);

if(count($results) > 0){

    foreach($results as $row3){

        //display them
     echo $row3['ket_nilai'] ;
     echo $row3['jum_nilai'];
    }
}else{

    echo "No data available";
}
?>