wagscd Posted May 13, 2021 Share Posted May 13, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312684-getquerycount-3-criterias/ Share on other sites More sharing options...
Barand Posted May 13, 2021 Share Posted May 13, 2021 (edited) 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 May 13, 2021 by Barand arguments for function calls Quote Link to comment https://forums.phpfreaks.com/topic/312684-getquerycount-3-criterias/#findComment-1586520 Share on other sites More sharing options...
wagscd Posted May 14, 2021 Author Share Posted May 14, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312684-getquerycount-3-criterias/#findComment-1586533 Share on other sites More sharing options...
Barand Posted May 14, 2021 Share Posted May 14, 2021 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... Incorrect use of prepared statements 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? The if() condition in the middle is waste of code 1 Quote Link to comment https://forums.phpfreaks.com/topic/312684-getquerycount-3-criterias/#findComment-1586539 Share on other sites More sharing options...
Solution wagscd Posted May 14, 2021 Author Solution Share Posted May 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312684-getquerycount-3-criterias/#findComment-1586546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.