PHP 和 'variable' mySQL 查询

标签 php mysql

我知道我在这方面做错了很多很多事情,但我不知道如何解决这个问题(除了进行许多单独的查询。

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/

相关文章:

php - 从数据库中搜索名称

PHP CSV 上传脚本,使用新行?

php - 如果特定行数据尚未在同一日期插入,如何检查和插入数据

php - 在 Doctrine_Query 中使用虚拟字段

mysql - 错误 1452 (23000) : Cannot add or update a child row: a foreign key constraint fails for existing tables

mysql - MySQL中如何使用触发事件来防止某个用户被删除

mysql - 当一个表有很多引用时,高效地查询表

php - 用于评分html5 js的圆形div栏

mysql - native MySQL 分区和基于后端的分区之间的性能差异?

mysql - Docker 容器无法连接到其 mysql 服务器(访问被拒绝)