【问题标题】:MySQL query is not running properlyMySQL 查询未正常运行
【发布时间】:2014-05-31 12:08:53
【问题描述】:

这是我运行 mysql 查询的代码,如果 $contactgroup is not null or empty 使用 ajax 运行查询,我将得到 $contactgroup 并使用 AND 运行查询,但现在它只是在没有和条件的情况下运行查询,我正在打印 $contactgroup 和$sql 它打印数据但它没有运行查询和条件,我不知道为什么这个不起作用,

<?php
$contactgroup=$_GET['contactgroup'];
print $contactgroup;

$sql  = "SELECT * FROM contact where isdeleted = 0 ";
print $sql;

if ($contactgroup !="" && $contactgroup !="Empty" ){

    print "contact group is null or empty";

    $contactgroup_exp = explode(',', $contactgroup);
    $sql .= " AND contactgroup in ('".implode("', '", $contactgroup_exp)."')";  

}

?>

现在我的打印查询结果是

SELECT * FROM contact where isdeleted = 0

如果 $contactgroup 不为 null 或为空,我想要这样的结果

SELECT * FROM contact where isdeleted = 0 AND contactgroup in ('xxxxxxx')

谁能告诉我哪里出错了,谢谢

【问题讨论】:

  • 您的消息contact group is null or empty 应该在else 子句中,而不是if 子句中。
  • 确保您发出的是 GET 请求,而不是 POST。请改用$_REQUEST['contactgroup'];
  • @Barmar no 我打印 $contactgroup,它不为空或为空打印传递的值,我不知道为什么它不打印查询
  • 你的代码对我有用:ideone.com/bcW2kM
  • 相信$_GET['contactgroup'] 遵循某些约定,您的代码对于 SQL 注入是开放的。清理您的输入数据!

标签: php mysql sql


【解决方案1】:
SELECT * FROM contact where isdeleted = 0 AND contactgroup IS NOT NULL;

【讨论】:

  • 如何测试联系人组是否与$_GET['contactgroup'] 中的联系人组匹配?
  • SELECT * FROM contact where isdeleted = 0 AND contactgroup IS NOT NULL and contactgroup in ('xxxxxxx')
  • 如果您使用IN 进行测试,则不需要IS NOT NULL 测试。如果是NULL,它不会匹配任何东西。
  • 好的。谢谢你的信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
相关资源
最近更新 更多