Bony Massele
Bony Massele

Reputation: 11

php return statement fails to return correct values,how to solve it?

I have created a public function to call and return commission rates. Everything works well except for one service. I want its return value to check for these values before assigning a commission rate:

SUM(CASE WHEN Amount<100000 then 650 ELSE 1000 END )

I tried: $TRA_TRAN_RATE=SUM( WHEN Amount<100000 then 650 ELSE 1000 END )

but it returns this error:

syntax error, unexpected 'Amount' (T_STRING)

Can anyone give me an idea how to solve it? Here's the function I made:

 //retrieve the commission rates
    public function com_rate($key){
        $free_rate = 0.04;
        $TRA_TRAN_RATE=SUM( WHEN Amount<100000 then 650 ELSE 1000 END )
        switch($key){
            case 'E_RATE': return 0.25;break;
            case 'T_RATE': return 0.01;break;
            case 'D_RATE': return 0.0075;break;
            case 'E_T_RATE': return 0.03;break;
            case 'ET_RATE': return $free_rate;break;
            case 'N_RATE': return $free_rate;break;
            case 'TRA_TRAN_RATE': return $TRA_TRAN_RATE;break;

//error come from here

Upvotes: 1

Views: 53

Answers (3)

Musa
Musa

Reputation: 2662

If I get you right, you are using some DB connections and retrieving data and so... I Thing your code must looks like this (Yes this connection is deprecated but you can investigate appropriate links and write with new way)

<?php
//retrieve the commission rates
    function com_rate($key){
        $free_rate = 0.04;
        $query = mysql_query("SELECT SUM( WHEN Amount < 100000 then 650 ELSE 1000 END ) sum FROM TABLE");
        $result = mysql_fetch_array($TRA_TRAN_RATE);
        $TRA_TRAN_RATE = $result['sum'];

        //or with this way
        //$TRA_TRAN_RATE = mysql_result("SELECT SUM( WHEN Amount < 100000 then 650 ELSE 1000 END ) sum FROM TABLE",1);

        switch($key){
            case 'E_RATE': return 0.25;break;
            case 'T_RATE': return 0.01;break;
            case 'D_RATE': return 0.0075;break;
            case 'E_T_RATE': return 0.03;break;
            case 'ET_RATE': return $free_rate;break;
            case 'N_RATE': return $free_rate;break;
            case 'TRA_TRAN_RATE': return $TRA_TRAN_RATE;break;
        }
    }

    echo com_rate('ET_RATE');

?>

Upvotes: 1

Beginner
Beginner

Reputation: 4153

wrap your sql aggregate function to a quote then add CASE before WHEN to correct your ternary condition in mysql

$TRA_TRAN_RATE=SUM( WHEN Amount<100000 then 650 ELSE 1000 END )

to

$TRA_TRAN_RATE='SUM( CASE WHEN Amount<100000 then 650 ELSE 1000 END )'

Upvotes: 0

jeroen
jeroen

Reputation: 91742

You seem to be mixing mysql and php; you are trying to assign a sql expression to a php variable:

$TRA_TRAN_RATE=SUM( WHEN Amount<100000 then 650 ELSE 1000 END )

In php you can use a ternary expression:

$TRA_TRAN_RATE = $Amount < 100000 ? 650 : 1000;

Assuming that the amount is stored in the $Amount variable. As that is undefined in the scope of your function, you would need to send it as an extra parameter / do a database query / etc.

Upvotes: 1

Related Questions