shahanas sulthan
shahanas sulthan

Reputation: 295

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.

$id = $row['ID'];

which shows

<p>Message:  Undefined index: DOCUMENT_ID</p>

how to solve this

Upvotes: 1

Views: 261

Answers (4)

Johnny2k
Johnny2k

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

harkirat1892
harkirat1892

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

Thamaraiselvam
Thamaraiselvam

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

shahanas sulthan
shahanas sulthan

Reputation: 295

I got the answer $Id = $row[0]['ID'];

Upvotes: 1

Related Questions