codeigniter:如何显示数据库中的第二行

时间:2013-12-01 06:39:47

标签: php codeigniter

我有一个网站。现在我想显示db的第二行。 这是我的模特

public function select_main_body() {
        $this->db->select('*');
        $this->db->from('tbl_main_body');
        $result_query = $this->db->get();
        $result = $result_query->row();
        return $result;

我用这个显示快行

<?php echo $main_body_details->main_body; ?>

提前致谢

4 个答案:

答案 0 :(得分:1)

试试这个

 public function select_main_body() {
            $this->db->select('*');
            $this->db->from('tbl_main_body');
            $result_query = $this->db->get();

            return $result_query ;

}

You call retrive this data 


foreach($result_query->result() as $row)
{
  print_r ($row);// you can retrive each record 

}

答案 1 :(得分:1)

您可以在行()

中包含rownum

public function select_main_body() {
    $this->db->select('*');
    $this->db->from('tbl_main_body');
    $result_query = $this->db->get();
    $result = $result_query->row(2);
    return $result;

答案 2 :(得分:1)

public function select_main_body() {
        $this->db->limit(1,1);
        return $this->db->get('tbl_main_body')->row();
}

如果我清楚你的问题,这将给你数据库的第二行。 或者

public function select_main_body() {
        return $this->db->get('tbl_main_body')->row(2);
}

答案 3 :(得分:0)

`$result_query->row()` into `foreach` and extract whatever row values you want to get. 
像这样:

foreach($result_query->result() as $row)
{
     echo $row['<name of the element you want to get>']
}

<强>更新

看到这里,我如何获取表格的不同字段,你会得到这个想法。我刚用while代替foreach

$con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');
            if (mysqli_connect_errno())
            {   
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
            return;
        }
        $result = mysqli_query($con,"SELECT * FROM userpost WHERE userid = '".$user_id."' order by time DESC");
        while ($row = @mysqli_fetch_array($result))
        {
            $title = $row['title'];
            $url = $row['url'];
            $preview = $row['preview'];
            $image = $row['img_url'];
            echo '<a class = "fragment" href = "'.$url.'" target = "_blank" >';
            echo '<div style="text-align: left;">';
            echo '<img src = "'.$image.'" height = "116" width = "116" alt = "some description"/>';
            echo '<span style="color:#7C7E7A;">'.$title.'</span>';echo '</br>';
            echo '<span style="">'.$url.'</span>'; echo '</br>';
            echo '<p class = "text" style="color:#85C5EB;" >'.$preview.'</p>';
            echo '</br>';
            echo '</div>';
            echo '</a>';
        }