在数据库表中没有行意味着获取错误未定义变量:使用codeigniter的结果

时间:2017-05-03 12:37:26

标签: php mysql codeigniter

在数据库表中没有行意味着获取错误未定义变量:使用codeigniter的结果,如果没有行或空表,则表示我想显示视图页

控制器

$data['breview'] = $this->Profile_model->review();
  $this->load->view('supplierreview', $data);

模型

公共职能评论(){

    $this->db->select('*');
    $this->db->from('reviews');
    $this->db->join('supplier_otherdetails', 'supplier_otherdetails.supplierid_fk = reviews.supplier_id');
     $this->db->join('customer_registration', 'reviews.customer_id=customer_registration.id');
     $this->db->join('sub3_category', 'reviews.product_id=sub3_category.id');
    $this->db->where('supplierid_fk', $this->session->id);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {

        $results = $query->result();
    }

    return $results;
}

查看页面

<?php
            foreach ($breview as $row) {
                ?>
                    <div class="reviewsection">

                        <img src="<?php echo 'data:image;base64,' .$row->product_image; ?>" class="img-circle img-user" alt="" width="50px;" height="50px;"/>           
                        <span class="starfont"><botton class="btn btn-danger"> <?php echo $row->ratings; ?> &nbsp;&nbsp;<span class="fa fa-star starfont"></span></botton> </span>
                        <div class="content-left"> 
                            <p><b>Product Name:<?php echo $row->product_name; ?></b></p>

                            <p><?php echo $row->review_msg; ?></p>   


                         <?php  $buyer_review = strtotime($row->review_date);?>
                            <?php $date=date('d-F-Y',$buyer_review); ?>

                            <p>Buyer Name : <?php echo $row->first_name; ?>  <?php echo $date ; ?></p>
                        </div>
                    </div>
            <?php } ?>

4 个答案:

答案 0 :(得分:2)

$ results未定义。

请添加$results = [];

你走了:

$results = []; 
if ($query->num_rows() > 0) {

    $results = $query->result();
}
return $results;

答案 1 :(得分:1)

在我的模型中,在大多数情况下,我会执行以下操作

public function my_function() {
    //$qry = YOUR_QUERY

    if ($qry->num_rows() > 0) //or ==1 or whatever, depends on your structure
        return $qry->result_array(); // or row_array, depends on your structure
    return FALSE;
}

然后在您的控制器中,您可以检查结果是FALSE还是EMPTY,如:

$result_from_model = $this->my_model->my_function();

if($result_from_model && !empty($result_from_model)) {
   //your code here
}

答案 2 :(得分:1)

您可以使用以下代码:

return (is_array($results)?$results:array()); 

希望它有效。

答案 3 :(得分:1)

到目前为止给出的所有答案都很好。这是另一种方法。它与使用三元而不是if的接受答案基本相同。

    $query = $this->db->get();
    return $query->num_rows() > 0 ? $query->result() : array();
}