Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a single value from CI model method

Tags:

codeigniter

I've got a method that's meant to return a single value. Because ->result() returns an array I'm using the following to return the single value I'd like to get:

return array_pop($this->db->query($SQL)->result())->event_name;

while it works perfectly fine, I'm wondering if there's something built into CI that maybe I've missed in the documentation. I also use this same trick to return a single record:

return array_pop($this->db->query($SQL)->result());

Is there a better way to do this?

like image 288
Will Avatar asked Sep 08 '26 02:09

Will


1 Answers

row_array() will return you the 1st row found as an array, and row() will return it as an object.

So instead of array_pop, you can use:

return $this->db->query($SQL)->row()->event_name;

OR

return $this->db->query($SQL)->row_array();

https://www.codeigniter.com/user_guide/database/results.html

like image 179
Rocket Hazmat Avatar answered Sep 13 '26 19:09

Rocket Hazmat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!