Adam
Adam

Reputation: 9449

Codeigniter modify database query array item

I'm attempting to modify a date field from my database before it gets outputted to the view, but I'm not having much luck. This code doesn't seem to work, what am I doing wrong?

function get_journal_entry($id)
    {
        $sql = 'SELECT * FROM journal WHERE user_id = '.$this->tank_auth->get_user_id().' AND id = '.$id;
        $query = $this->db->query($sql);

        $query['created'] = date("c", strtotime($query['created']));


        return $query->row_array();
    }

Upvotes: 0

Views: 1516

Answers (2)

gen_Eric
gen_Eric

Reputation: 227270

$this->db->query returns a query object, not your results. You need to modify the row after calling $query->row_array.

$query = $this->db->query($sql);
$result = $query->row_array();
$result['created'] = date("c", strtotime($result['created']));
return $result;

Upvotes: 3

J. Bruni
J. Bruni

Reputation: 20492

Another version of the code, which may work:

function get_journal_entry($id)
{
    $sql = 'SELECT * FROM journal WHERE user_id = ' . intval($this->tank_auth->get_user_id()) . ' AND id = ' . intval($id);
    $row = $this->db->query($sql)->row_array();
    $row['created'] = date("c", strtotime($row['created']));
    return $row;
}

Upvotes: 1

Related Questions