Reputation: 295
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.
$id = $row['ID'];
which shows
<p>Message: Undefined index: DOCUMENT_ID</p>
how to solve this
Upvotes: 1
Views: 261
Reputation: 163
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. You can also use
$row = $this->db->query($sql)->row_array();
$id = $row['id'];
Upvotes: 0
Reputation: 463
If the id is a unique key in your database, as it seems, you should do this:
$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.
To get id, simply do:
$id = $row['ID'];
Just an optimized way as compared to the solution you have found.
Upvotes: 0
Reputation: 7080
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;
}
Upvotes: 0