[英]retrieving value from codeigniter database array
i am using 我在用
$sql= "select * from tablename where id=1";
$query= $this->db->query($sql);
$row = $query->result_array();
in model of codeigniter.My table consists of a field name 'ID'.how to retrive ID from $row. 在codeigniter模型中。我的表由一个字段名'ID'组成。如何从$ row中检索ID。
$id = $row['ID'];
which shows 这表现了
<p>Message: Undefined index: DOCUMENT_ID</p>
how to solve this 如何解决这个问题
我得到了答案$ Id = $ row [0] ['ID'];
I hope this might help you 希望对您有帮助
$sql= "select * from tablename where id=1";
$query= $this->db->query($sql);
foreach($query->result() as $values){
echo $values->id;
}
If the id is a unique key in your database, as it seems, you should do this: 如果看起来该id是数据库中的唯一键,则应执行以下操作:
$sql= "select * from tablename where id=1";
$row = $this->db->query($sql)->row();
It fetches the first row of the result. 它获取结果的第一行。 As id is unique, you only want to fetch one row too. 由于id是唯一的,因此您也只想获取一行。
To get id, simply do: 要获取ID,只需执行以下操作:
$id = $row['ID'];
Just an optimized way as compared to the solution you have found. 与您找到的解决方案相比,这只是一种优化的方法。
If you get the row as 如果您得到的行为
$row = $this->db->query($sql)->row();
$id = $row->id;
you get the first row as an object and should access it as $row->id. 您将第一行作为对象,应以$ row-> id的形式访问它。 You can also use 您也可以使用
$row = $this->db->query($sql)->row_array();
$id = $row['id'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.