开发者

CodeIgniter - Returning column result instead of entire row

I have a query which im trying to return the exact entry for, but instead it keeps returning the 'Array' ... how do i extract just the one 'name' in looking for?

My current query looks like so

$query = $this->db->query('SELECT name FROM '.$this->table_name.' WHERE id =1');
return $query->result();

Basically, i just want t开发者_JS百科o return the actual name, not an array


You need to dig into the objects returned by the query. row() returns the first row in a result set:

$row = $query->row();
return $row->name;

result() returns an array of objects representing the rows in the result set. You need to get the object itself, then get it's 'name' property.

It's also worth checking $query->num_rows() to make sure you have results:

if ($query->num_rows() > 0) {...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜