0

I'm using PHP with Mysqli and I'm trying to avoid duplicated results from the Database. However, the count result is different from these two very similar code. I'ts the first time I'm using DISTINCT, so I would like to know what's causing the difference. Thank you!

Ps: I saw similar question but the sql command is different.

Count: 83

SELECT DISTINCT (`email`) `id`,`name`,`email`,`sent`,`datasent` 
FROM $dbtable_requests 
WHERE `datasent`>'$datedb' 
  AND category = '$cat'  
ORDER BY `name`

Count: 77

SELECT DISTINCT (`email`) `id` 
FROM $dbtable_requests 
WHERE `datasent`>'$datedb' 
  AND category='$cat_q'
  • 1
    So whats in `$cat` and `$cat_q` as the variable names are different, are the contents also different? – RiggsFolly Apr 18 '17 at 16:13
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Apr 18 '17 at 16:13
  • have you tried with GROUP BY – Peter Apr 18 '17 at 16:13
  • 1
    they are in different functions that's why the $cat and $cat_q – Guilherme Nunes Apr 18 '17 at 16:14
  • @RiggsFolly, thanks I will take a look. – Guilherme Nunes Apr 18 '17 at 16:16
  • @Csimpi, I will try here – Guilherme Nunes Apr 18 '17 at 16:16

0 Answers0