我知道我在这方面做错了很多很多事情,但我不知道如何解决这个问题(除了进行许多单独的查询。
mysql_query( 'SELECT *
FROM opdracht
where'. if(isset($opleiding))
{
'opleiding = "'.$opleiding.'" and'
}
if(isset($duur))
{
'duur = "'.$duur.'" and '
}
if(isset($type))
{
'type = "'.$type.'" and'
}
' gevuld ="nee";');
基本上,我要做的是查询哪些语句取决于变量。
这段代码不起作用,但我想不出让它起作用的方法。
帮忙?
最佳答案
if (isset($opleiding)) { $opleiding = 'opleiding = "'.$opleiding.'" and'; } else { $opleiding = ""; }
if (isset($duur)) { $duur= 'duur= "'.$duur.'" and'; } else { $duur= ""; }
if (isset($type)) { $type= 'type= "'.$type.'" and'; } else { $type= ""; }
$query = "SELECT * FROM optdracht WHERE $oplediding $duur $type gevuld ='nee';"
mysql_query($query);
在外部设置它们,然后在查询内部使用它们。此外,您应该检查错误。 (我不记得如何在 mysql_query()
中执行此操作,因为它很旧而且我不使用它。如果有人知道请编辑我的答案)
关于PHP 和 'variable' mySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7686117/