Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP mysql选择查询where(通配符)_Php_Mysql_Wildcard - Fatal编程技术网

PHP mysql选择查询where(通配符)

PHP mysql选择查询where(通配符),php,mysql,wildcard,Php,Mysql,Wildcard,我试图使用下面的MySQL查询,但显然有问题。我试图使page.php或page.php?q=返回以下查询: if (!isset($_GET['q'])) { $where = "WHERE column1 LIKE %"; } else { $where = "WHERE column1 LIKE ".$_GET['q']; } $query = "SELECT column1, column2 FROM table '$where' GROUP BY column1"; 因此,如果没有

我试图使用下面的MySQL查询,但显然有问题。我试图使page.phppage.php?q=返回以下查询:

if (!isset($_GET['q'])) { $where = "WHERE column1 LIKE %"; }
else { $where = "WHERE column1 LIKE ".$_GET['q']; }

$query = "SELECT column1, column2 FROM table '$where' GROUP BY column1";
因此,如果没有GET,那么MySQL查询中就没有
WHERE
。如果GET设置为某个值,那么就有一个带有该GET值的
WHERE

目前,我遇到以下错误:

您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 在第1行的“WHERE column1LIKE”附近按column1分组


你需要用某种方式逃避,但那是另一天的练习。如果只是想让它工作,请删除where变量周围的单引号

$query = "SELECT column1, column2 FROM table $where GROUP BY column1";

你需要用某种方式逃避,但那是另一天的练习。如果只是想让它工作,请删除where变量周围的单引号

$query = "SELECT column1, column2 FROM table $where GROUP BY column1";

我想你可以这么做: 顺便提一下您不需要“like%”的另一部分,您可以简单地将where子句一起省略,它将产生相同的效果。。。 这是您刚刚发布的内容的副本:

$where = "";
//if there the get q is set we add the where clause
if(isset($_GET['q'])) { 
   $where = "WHERE column1 LIKE '".$_GET['q']."'"; 
}

$query = "SELECT column1, column2 FROM table ".$where." GROUP BY column1";

我想你可以这么做: 顺便提一下您不需要“like%”的另一部分,您可以简单地将where子句一起省略,它将产生相同的效果。。。 这是您刚刚发布的内容的副本:

$where = "";
//if there the get q is set we add the where clause
if(isset($_GET['q'])) { 
   $where = "WHERE column1 LIKE '".$_GET['q']."'"; 
}

$query = "SELECT column1, column2 FROM table ".$where." GROUP BY column1";

您需要将搜索字符串放在单引号之间的
WHERE
子句中,如下所示:

$where = "";
// if there the get q is set we add the were clause
if (!isset($_GET['q'])) {
    $where = "WHERE column1 LIKE %";
    // ^ This WHERE clause is useless, since it matches all strings.
    // Omitting this WHERE clause has the same effect.
}
else { $where = "WHERE column1 LIKE ".$_GET['q']; }

$query = "SELECT column1, column2 FROM table ".$where." GROUP BY column1";

注意您的脚本非常容易受到攻击

您需要将搜索字符串放在单引号之间的
WHERE
子句中,如下所示:

$where = "";
// if there the get q is set we add the were clause
if (!isset($_GET['q'])) {
    $where = "WHERE column1 LIKE %";
    // ^ This WHERE clause is useless, since it matches all strings.
    // Omitting this WHERE clause has the same effect.
}
else { $where = "WHERE column1 LIKE ".$_GET['q']; }

$query = "SELECT column1, column2 FROM table ".$where." GROUP BY column1";

注意您的脚本非常容易受到攻击

对于使用的通用解决方案,请尝试以下代码段(其中$db是PDO连接对象)


对于使用的通用解决方案,请尝试以下代码段(其中$db是PDO连接对象)


您的代码极易受到SQL注入攻击。请使用参数化查询。谢谢
Dai
。目前这只是一个临时页面。我不会呆太久,而且只在内联网上。您的代码对SQL注入攻击非常开放。请使用参数化查询。谢谢
Dai
。目前这只是一个临时页面。我不会很长时间起床,只在内联网上。谢谢,我更新了我的PDO脚本,上面的内容帮助了我的问题。谢谢,我更新了我的PDO脚本,上面的内容帮助了我的问题。