CI无法输出我的联合表格

时间:2014-12-01 05:58:54

标签: codeigniter

我有三个表book_tbl(book_ID,title); isbn_tbl(isbn_id,isbn_number,book_ID // foriegn key),最后是author_tbl(author_ID,author_name,book_ID // foriegn key)

model:
 function getRecord(){
    $data = array(
        'book_tbl.title',
        'author_tbl.author_name',
        'isbn_tbl.isbn_number'
        );
    $this->db->select($data);
    $this->db->from('book_tbl');
    $this->db->join('author_tbl','author_tbl.book_ID=book_tbl.book_ID','inner');
    $this->db->join('isbn_tbl','book_tbl.book_ID=isbn_tbl.book_ID','inner');
    $this->db->order_by('book_tbl.book_ID','ASC');
    return $query = $this->db->get()->result();
}

controller:
public function display_book(){

    $data = array();
    if($query = $this->module->getRecord()){

        $data['result'] = $query;

    }

    #$this->success($data);
}

view:
<?php if(isset($result) && ($result->num_rows() > 0)) : foreach ($result as $row) : ?>
<?php 
echo $row->title;
echo $row->author_name;
echo $row->isbn_number;
?>
<?php endforeach; ?>

<?php else : ?>

<h2>No Record</h2>

<?php endif; ?>

2 个答案:

答案 0 :(得分:0)

function getRecord(){
$data = array(
    'book_tbl.title',
    'author_tbl.author_name',
    'isbn_tbl.isbn_number'
    );
$this->db->select($data);
$this->db->from('book_tbl');
$this->db->join('author_tbl','author_tbl.book_ID=book_tbl.book_ID','inner');
$this->db->join('isbn_tbl','book_tbl.book_ID=isbn_tbl.book_ID','inner');
$this->db->order_by('book_tbl.book_ID','ASC');
$query = $this->db->get();
//echo query here by $this->db->last_query();
}

$this->db->last_query();回复你最后的查询副本并将其粘贴到你的mysql中并检查数据是否即将到来?

如果是,则检查模型中的 num_rows(),并根据该显示结果,因为它将是维护良好的代码。

答案 1 :(得分:0)

因此您在查询中没有错误,因此请在MySQL中首先进行查询,如果输出随后集成在CodeIgniter模型中

function getRecord(){
$data = array(
'book_tbl.title',
'author_tbl.author_name',
'isbn_tbl.isbn_number'
);
$this->db->select($data);
$this->db->from('book_tbl');
$this->db->join('author_tbl','author_tbl.book_ID=book_tbl.book_ID');
$this->db->join('isbn_tbl','isbn_tbl.book_ID=book_tbl.book_ID');
$this->db->order_by('book_tbl.book_ID','ASC');
$res = $this->db->get();
    if ($res->num_rows > 0) {
        return $res->result();
    } else {
        return false;
    }

}

尝试这可能会有所帮助