[英]PHP MySQL Where clause isn't working
I have a database like which has multiple columns and when querying it with a WHERE
clause it won't get any results. 我有一个具有多个列的数据库,当用WHERE
子句查询它时,不会得到任何结果。 Here is the code I am using : 这是我正在使用的代码:
$columns = $_GET['var'];
$where = $_GET['where'];
$checkValue = $_GET['checkValue'];
$userInput = $_GET['userInput'];
$query = "SELECT ";
foreach($columns as $val)
$query .= "$val, ";
$query .= "FROM Email";
if($where === "yes")
$query .= " WHERE $checkValue = '$userInput'";
$columns
is multiple checkboxes for the user to select which columns they wish to see. $columns
是多个复选框,供用户选择他们希望查看的列。 It works perfectly except when adding the where clause. 除非添加where子句,否则它运行完美。 When I've been testing it I made sure that the it was exactly the same as in the database. 当我进行测试时,我确保它与数据库中的完全相同。 Also the $checkValue
is a dropdown list which values are exactly the same as in the database. $checkValue
也是一个下拉列表,其值与数据库中的值完全相同。 Also just to note later on I edit the query so the last comma is removed. 还要注意稍后我编辑查询,以便删除最后一个逗号。
To print it out I use : 要打印出来,我使用:
while($c = mysqli_fetch_assoc($results)){
foreach($columns as $val){
$header = ucwords($val);
echo "<b>$header</b><br>";
echo $c[$val]."<br>";
}
echo "-------------------------------<br>";
}
This is the query that is outputted when not using the where clause and works: 这是不使用where子句时输出的查询,并且有效:
SELECT date, mediatype FROM Email
And here is the query that doesnt work: 这是不起作用的查询:
SELECT date, mediatype FROM Email WHERE mediatype = 'Blog'
Any advice? 有什么建议吗?
EDIT: Here is the table with: 编辑:这是与表:
There is more columns but these are ones I want to focus on. 还有更多列,但我想重点关注这些列。
Your generate SQL request seems to have a syntax error. 您的生成SQL请求似乎存在语法错误。 Just change the way you generate it. 只需更改生成方式即可。
Instead of 代替
foreach($columns as $val)
$query .= "$val, ";
Try 尝试
$query .= implode(', ' $columns);
That will skip the last comma. 那将跳过最后一个逗号。
The blog column had an extra empty line that 'Blog%' wasn't working on. Blog列还有一个多余的空行,“ Blog%”无法使用。 I went in the database and deleted the extra line and used the query again and it worked. 我进入数据库并删除了多余的行,然后再次使用该查询,它就可以了。
Thanks everyone for the help :) 谢谢大家的帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.