I am attemping to calculate the cost by issuing the query below however it always return no value:

        //Re-calculate the cost price
        $f15 = "SELECT sum(cost) FROM ingredients WHERE item_id_fk = :targetID";
        $e16 = $this->l1->prepare($f15);
        $e16->bindParam(':targetID', $g4);
        $e16->execute();
        $g5 = $e16->fetch(PDO::FETCH_NUM);

        error_log(print_r($g5[0], TRUE));

Did I miss out something? Your help is kindly appreciated.

Thank You.

Recommended Answers

All 6 Replies

Try to remove the WHERE condition, just to see if the query returns something and what is assigned to :targetID:

$f15 = "SELECT sum(cost), :targetID as 'target' FROM ingredients";

And in output do:

error_log(print_r($g5, TRUE));

So you get the full array.

It returns the total sum for the column cost.

Ok, but the $g4 value is sent to the query? And if you perform the original query directly into a mysql client, do you get results?

Member Avatar for diafol

I'd var_dump( $g4 ) to see what you get. And as cereal says, hardcode the value into the SQL WHERE clause to see if you get anything returned.

commented: +1 +13

The solution is to access the value in the array by column name as follow:

$e16->bindParam(':targetID', $g4['itemId']);
Member Avatar for diafol

Heh heh, of course it is. It was obvious that $g4 was an array! :rolleyes:
BTW - why all the short names? Don't you get confused?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.