Welcome Guest, Not a member yet? Register   Sign In
How to return single result from database
#1

[eluser]philstan[/eluser]
I know this is a laughably newbie question, but I just can't find an answer and would love some help.

I have this SQL statement in my model and obviously I'm looking to return a single value and not an array. How do I do it please as this is still returning an array?

Code:
function get_dollars_by_month ($month, $donator_co)
    {
        $query = $this->db->query("SELECT SUM( dollars )
        FROM fh_financial_donations
        WHERE MONTH( date ) = MONTH( DATE_SUB( CURDATE( ) , INTERVAL ".$month."
        MONTH ) ) AND donator_co = ".$donator_co."");
        if ($query->num_rows() == 1) {
        return $query->row();
      } else {
        return FALSE;
      }
    }


Which returns:

Code:
stdClass Object ( [SUM( dollars )] => 398 )

Would love to NOT have to access an array. However, I'm not sure how to access this array anyway...

Thanks, philstan
#2

[eluser]slowgary[/eluser]
I don't think there's any way around it. Since you're querying a database table, you're always getting a row back (an array). Even if there's only 1 field in the row it still comes back as an array.
#3

[eluser]philstan[/eluser]
That's irritating. However, how do I access the 398 from this result:

stdClass Object ( [SUM( dollars )] => 398 )

I tried $result->row([SUM( dollars )]);

But that's jibberish.

Thx Philstan
#4

[eluser]slowgary[/eluser]
I've always felt the same way, if your query only selects 1 columns and has a limit 1 it would be nice to only get the single value back. It makes sense though that mysql is always going to return a results object.
#5

[eluser]philstan[/eluser]
Yes indeed.

BUT please let me know how to extract my data from this array which was returned from the function above:

stdClass Object ( [SUM( dollars )] => 398 )

I need the 398, but I'm not sure of the correct syntax to get it out.
#6

[eluser]Thorpe Obazee[/eluser]
Code:
// model
function get_dollars_by_month ($month, $donator_co)
    {
        $query = $this->db->query("SELECT SUM( dollars )
        AS thesum FROM fh_financial_donations
        WHERE MONTH( date ) = MONTH( DATE_SUB( CURDATE( ) , INTERVAL ".$month."
        MONTH ) ) AND donator_co = ".$donator_co."");
        return ($query->num_rows()) ? $query->row() : FALSE
    }

//Controller

$data = $this->yourmodel->get_dollars_by_month($month, $donator_co);

echo $data->thesum;

or you can do this:

Code:
// model
function get_dollars_by_month ($month, $donator_co)
    {
        $query = $this->db->query("SELECT SUM( dollars )
        AS thesum FROM fh_financial_donations
        WHERE MONTH( date ) = MONTH( DATE_SUB( CURDATE( ) , INTERVAL ".$month."
        MONTH ) ) AND donator_co = ".$donator_co."");
        return ($query->num_rows()) ? $query->row()->thesum : FALSE
    }

//Controller

$thesum = $this->yourmodel->get_dollars_by_month($month, $donator_co);

echo $thesum;
#7

[eluser]rameners[/eluser]
access it through it's column name.

in the view pages or controller try to access it using these syntax, just modify the arrayname for what you are using.

echo $arrayname[0]['SUM( dollars )'];

i was here,

ramener
#8

[eluser]philstan[/eluser]
Thank you Gentlemen.

That's very helpful




Theme © iAndrew 2016 - Forum software by © MyBB