我正在尝试使用下面的if/else语句来构建查询,但我在第一步中对NULL求值时出现问题。无效的MySQL资源
经过一些在线搜索后,我似乎无法追查我做错了什么......应该使用空引号代替:""
?
虽然这也给了我一个错误。
所以我不知道问题是在第一个块还是第二个,即while
循环。
有什么建议吗?
$name = $_POST['Your_name'];
if ($location != "All" && $name == NULL) $query="SELECT * FROM talent WHERE duty_station='$location')";
else if($location == "All" && $name != "All") $query="SELECT * FROM talent WHERE Your_name IN ('$name')";
else if($dutyReq != "All" && $name == "All") $query="SELECT * FROM talent WHERE duty_station='$location'";
else if($location == "All" || $name == "All") $query="SELECT * FROM talent";
然后我的循环,打印出的数据给了我这个错误:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/talent/searchresults.php on line 64
这是错误来自代码:
$result=mysql_query($query);
mysql_query($result);
echo "<table border='1'>
<tr>
<th>Your name</th>
<th>duty station</th>
<th>first proficiency</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['Your_name'] . "</td>";
echo "<td>" . $row['duty_station'] . "</td>";
echo "<td>" . $row['prof_order_processing'] . "</td>";
echo "</tr>";
}
echo "</table>";
看起来你在这里的SQL语句有错误“SELECT * FROM talent WHERE duty_station ='$ location'----->)” –
**你的代码容易受到SQL注入的攻击**你真的*应该使用[准备语句](http://stackoverflow.com/a/60496/623041),将变量作为参数传递到其中,而这些参数不针对SQL进行评估。如果你不知道我在说什么,或者如何解决它,请阅读[Bobby Tables]的故事(http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain) 。 – eggyal
在PHP中,'null'是小写字母。 –