Billk Billk - 1 month ago 6
MySQL Question

SQL Count with PHP Session Variable

I have a SQL in php wich acts fine:

"SELECT Aflevering1, COUNT(1) AS Gekozen FROM widm GROUP BY Aflevering1";.


It returns this table:

Aflevering1 | Gekozen



Kandidaat1 | 1



Kandidaat2 | 2



I want to use a session variable in the SQL. What I have now is the following:
"SELECT '".$_SESSION['afl']."', COUNT(1) AS Gekozen FROM widm GROUP BY '".$_SESSION['afl']."' "


When the session variable is 'Aflevering1', the result is something different than above:

Aflevering1 | Gekozen



Aflevering1 | 3



What am I doing wrong?

Answer

Because you are passing string into your query, not the session variable

"SELECT '".$_SESSION['afl']."', COUNT(1) AS Gekozen FROM widm GROUP BY '".$_SESSION['afl']."' "

will generate this query:

SELECT  'Aflevering1' , COUNT(1) AS Gekozen FROM widm GROUP BY  'Aflevering1'