带有PHP会话变量的SQL计数

时间:2016-11-01 08:00:51

标签: php mysql mysqli

我在PHP中有一个SQL,行为很好:

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

它返回此表:

Aflevering1 |   Gekozen
------
Kandidaat1 |    1
------
Kandidaat2 |    2
------

我想在SQL中使用会话变量。我现在拥有以下内容:

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

当会话变量为'Aflevering1'时,结果与上述不同:

Aflevering1 |   Gekozen
-----
Aflevering1 |   3
-----

我做错了什么?

1 个答案:

答案 0 :(得分:-1)

因为您将字符串传递给查询,而不是会话变量

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

将生成此查询:

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