Welcome Guest, Not a member yet? Register   Sign In
Return value of select query with orderby and limit is producing error
#1

[eluser]johnwbaxter[/eluser]
I have in my model:

$this->db->select('orderid');
$this->db->from('orders');
$this->db->where('cust_id =', $cust_id);
$this->db->order_by('orderid', 'DESC');
$this->db->limit(1);
$lastorder = $this->db->get();
return $lastorder;

Then in my controller i have:

$orderid = $this->ordermodel->retorder();

And then i have a line of code that tries to use the $orderid variable to populate the value of a form field.

In my log i have this error:

Severity: Notice --> Object of class CI_DB_mysql_result to string conversion /var/www/vhosts/mysite.com/httpdocs/system/helpers/form_helper.php 360

Any ideas anyone?!

I'm totally stumped. I think it's returning an object instead of a value but i'm not sure why or how to access it if it is in this scenario.
#2

[eluser]Armchair Samurai[/eluser]
You may have done this somewhere else in your code, but you need to get the result from your query, like:
Code:
$lastorder = $this->db->get();
$result = $lastorder->row();
return $result->orderid;
#3

[eluser]johnwbaxter[/eluser]
Right, okay, i see.

I was trying to access the object directly like $lastorder->orderid; without going $result = $lastorder->row(); first.

Is the best way to do this? I'm sure there must be a way of doing this on less code.

Thank you very much for the reply and the assistance, it has worked excellently.

Cheers!!




Theme © iAndrew 2016 - Forum software by © MyBB