Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter上只返回一条记录_Php_Database_Codeigniter - Fatal编程技术网

Php 在codeigniter上只返回一条记录

Php 在codeigniter上只返回一条记录,php,database,codeigniter,Php,Database,Codeigniter,如何在此语句中仅返回一条记录 public function edititem($id){ $this->db->select('*'); $query = $this->db->get('tblitem'); $this->db->where('item_id',$id); foreach ($query->result() as $row){ echo $row->item_id.'</br>

如何在此语句中仅返回一条记录

public function edititem($id){
   $this->db->select('*');
   $query = $this->db->get('tblitem');
   $this->db->where('item_id',$id);

   foreach ($query->result() as $row){
        echo $row->item_id.'</br>';
        echo $row->item_name.'</br>';
        echo $row->item_description.'</br>';
        echo $row->item_price.'</br>';
   }
}
公共函数编辑项($id){
$this->db->select('*');
$query=$this->db->get('tblitem');
$this->db->where('item_id',$id);
foreach($query->result()作为$row){
echo$row->item_id.
; echo$row->item_name.
; echo$row->item_description.'
'; echo$row->item_price.
; } }
它给我所有的记录,而不是使用
$this->db->limit(1)
仅获取1条记录。

使用这些

方法01-

方法02-

方法03-


这些将只生成一组数据。在您的
foreach
循环中,它将始终返回一个用于获取单行表单表use
->row()

函数编辑项($id){
$this->db->select('item_id,item_name,item_description,item_price');
$this->db->where('item_id',$id);
$this->db->limit(1);//仅当您的表中有多个相同的id时应用,否则请对此行进行注释
$query=$this->db->get('tblitem');
$row=$query->row();
echo$row->item_id.
; echo$row->item_name.
; echo$row->item_description.'
'; echo$row->item_price.
; }

阅读使用
limit
,这样sql将返回找到的第一行,而不是整组行。最后一个问题是如何取得结果

以下是两个单套解决方案,通过:

若以后需要引用多个值,请将row()的结果赋给变量,以便以后重用

$row = $this->db->limit(1)->get('tblitem')->row();
使用以下命令:

$this->db->get()->custom_row_object(0, 'Your_Model');

codeigniter用户指南中提到了上述代码。最好的方法是不需要循环foreach,在这里可以得到非常具体的行值。

只需添加
$this->db->select('column_name')
并使用
->row()
获取单个记录以及返回的单个值???我的意思是单个记录而不是单个值。我想知道一件物品的名称、描述和价格,如果你有多件物品具有相同的id??
$row = $query->first_row('array');
$row = $query->last_row('array');
$row = $query->next_row('array');
$row = $query->previous_row('array');
function edititem($id) {
    $this->db->select('item_id,item_name, item_description,item_price');
    $this->db->where('item_id', $id);
    $this->db->limit(1);// only apply if you have more than same id in your table othre wise comment this line
    $query = $this->db->get('tblitem');
    $row = $query->row();

    echo $row->item_id . '</br>';
    echo $row->item_name . '</br>';
    echo $row->item_description . '</br>';
    echo $row->item_price . '</br>';
}
// ... other query conditions
$this->db->limit(1)->get('tblitem')->row()->your_key;

// or with the short syntax for defining limit, through the get:
$this->db->get('tblitem', 1, 0)->row()->your_key;
$row = $this->db->limit(1)->get('tblitem')->row();
$this->db->get()->custom_row_object(0, 'Your_Model');