How to get single row data in CI

Result Rows

Row() returns a single result row data.  If query has more than one row, it returns only the first row of data. The result is returned as an object. Example:-

$query = $this->db->query(“YOUR QUERY”);

$row = $query->row();

if (isset($row))
{
echo $row->title;
echo $row->name;
}
If you want data in array format use row_array()
Example:

$query = $this->db->query(“YOUR QUERY”);

$row = $query->row_array();

if (isset($row))
{
echo $row[‘title’];
echo $row[‘name’];
echo $row[‘body’];
}

Generating Query Results as Row in CI

What is difference between result and result_array in CI

if we use result()  to get results it will returns  the query result as an array of objects
$query = $this->db->query(“YOUR QUERY”);

foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}

if we use result_array()  to get result as a pure array
$query = $this->db->query(“YOUR QUERY”);

foreach ($query->result_array() as $row)
{
echo $row[‘title’];
echo $row[‘name’];
echo $row[‘body’];
}

 
Generating Query Results in CI