简体   繁体   English

从Codeigniter中的数据库检索结果

[英]Retrieving result from database in codeigniter

As I know so far, we can use 据我所知,我们可以使用

$result = $query->row_array();

to retrieve the single value from the database and 从数据库中检索单个值,然后

$result = $query->result_array();

to retrieve multiple values from the database. 从数据库中检索多个值。

In this case I am using the particular table CRUD options. 在这种情况下,我将使用特定的表CRUD选项。 So instead of writing 所以不要写

$rows = $query->num_rows();
if( $rows > 1 ){
    $result = $query->result_array();
}
if( $rows == 1 ){
    $result = $query->row_array();
}

Is there any other way to write the query even if the table has only a single row or multiple row? 即使表只有单行或多行,还有其他写查询的方法吗?

Please guide me. 请指导我。 Thanks in advance friends 在此先感谢朋友

You can try this way 你可以这样尝试

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
      echo $row->title;
      echo $row->name;
      echo $row->body;
   }
}

if you want check one row or multiple row 如果要检查一行还是多行

 $query = $this->db->query("YOUR QUERY");

    if ($query->num_rows() == 1)
    {
       foreach ($query->result() as $row)
       {
          echo $row->title;
          echo $row->name;
          echo $row->body;
       }
    }else if($query->num_rows() > 1){
       foreach ($query->result() as $row)
       {
          echo $row->title;
          echo $row->name;
          echo $row->body;
       }
}else{
    echo 'No any records in data base';
}

it's doesn't matter one record or more 一个或多个记录都没有关系

Try this: 尝试这个:

$query = $this->db->query("YOUR QUERY");
if ($query->num_rows() > 0)
{
    return $query->result();
}else{
    return false;
}
$result=$this->db->count_all("table_name")>1?$this->db->get("table_name")->result_array():$this->db->get("table_name")->row_array();
echo "<pre>";
print_r($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM