Jump to content

getquerycount 3 criterias


wagscd
Go to solution Solved by wagscd,

Recommended Posts

Hi all,

I have a code for count number of Long trade/

This code count the "long"

$count_long_trades = $db->getQueryCount('tbl_trades','lg_short_trade',' AND lg_short_trade="Long" AND num_bot_trade="'.$_REQUEST['editId'].'"');

It works perfectly.

 

Now i need to add a criteria and i'm totally lost ;)

I have try this but not works

The same code but with the count of only the positive PnL

$count_long_trades_pos = $db->getQueryCount('tbl_trades','lg_short_trade',' AND lg_short_trade="Long" ','pnl_trade',' AND pnl_trade > 0 AND num_bot_trade="'.$_REQUEST['editId'].'"');

Thanks by advance for your help.

Have a good day

Link to comment
Share on other sites

I have no idea how the function getQueryCount() is defined or what arguments it expects, but you appear to be passing 3 arguments in the one that works and 4  5 in the one that fails. Check your reference manual.

Arguments...

Array
(
    [0] => tbl_trades
    [1] => lg_short_trade
    [2] =>  AND lg_short_trade="Long" AND num_bot_trade="42"
)
Array
(
    [0] => tbl_trades
    [1] => lg_short_trade
    [2] =>  AND lg_short_trade="Long" 
    [3] => pnl_trade
    [4] =>  AND pnl_trade > 0 AND num_bot_trade="42"
)

 

Edited by Barand
arguments for function calls
Link to comment
Share on other sites

HI Barand,

Thanks for your anwser.

Here is this function

 

 public function getQueryCount($tableName, $field, $cond='')
    {
        $stmt = $this->pdo->prepare("SELECT count($field) as total FROM $tableName WHERE 1 ".$cond);
        try {
            $stmt->execute();
            $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
           
            if (! $res || count($res) != 1) {
               return $res;
            }
            return $res;
        } catch (\PDOException $e) {
            throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
        }
    }

I have try to compare with another function but i don't know why this one don't works

 

Thanks by advance for your help

Have a good day

Link to comment
Share on other sites

The function definition clearly shows that it takes only 3 arguments - table, field, condition. In your second call you are passing 5 so the final 2 are ignored.

Try

$count_long_trades_pos = $db->getQueryCount('tbl_trades','lg_short_trade',' AND lg_short_trade="Long" AND pnl_trade > 0 AND num_bot_trade="'.$_REQUEST['editId'].'"');

Secondly, I don't know what the other functions in your database class are like but that one should be consigned to the trash bin...

  1. Incorrect use of prepared statements
  2. The query will wil return one row with one column (the count), so
    why return fetchAll() which is for multiple rows?, and
    why not just return the record count?
  3. The if() condition in the middle is waste of code
  • Thanks 1
Link to comment
Share on other sites

  • Solution

A very BIG Thanks.  That works

For the second point, to be honnest, i don't know.  I have find a script like this.

So i make just the "copy cat" :):):)  I'm a beginner.

I will try to understand your advice.

Thanks so much.

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.