CodeIgniter中的数据库查询

时间:2016-01-09 10:11:51

标签: php html codeigniter

我有一个名为“news”的表,其中包含以下详细信息: id,title,slug,text

因此,我使用以下代码从我的数据库中检索整个“新闻”表。

public function get_news($slug = FALSE)
        {
            if ($slug === FALSE)
            {
                $query = $this->db->get('news');
                return $query->result_array();
            }

            $query = $this->db->get_where('news', array('slug' => $slug));
            return $query->row_array();
        }

显示结果

<?php foreach ($news as $news_item): ?>

                <h3><?php echo $news_item['title']; ?></h3>
                <div class="main">
                        <?php echo $news_item['text']; ?>
                </div>
                <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>

        <?php endforeach; ?>

我应该如何更改代码以获取“从新闻中选择标题,文字”的查询结果?

1 个答案:

答案 0 :(得分:2)

试试这个

$query = $this->db->select('title, text,')->from('news')->get();

where

 $query = $this->db->select('title, text,')->from('news')->where('slug', $slug)->get();