如何使用';喜欢';运算符来检索使用$_GET值的查询结果


How to use 'LIKE' operator in MySQL to retrieve result of a query using the value of $_GET

我想查询我的数据库,以便它显示基于我的PHP超全局$_GET的查询结果。我试过这个:

LIKE '%".$_GET["name"]."%'"

LIKE '%{$_GET["name"]}%' 

然而,这是徒劳的。有人能帮我吗?

这是我的php代码:

$places = query( "SELECT * FROM places WHERE MATCH (postal_code, country_code, admin_name1, admin_code1, place_name) AGAINST (?) OR LIKE '%".$_GET["geo"]."%'", $_GET["geo"]);

错误消息显示:

Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%akutan%''

在mysql查询中直接使用全局变量不是一个好主意。

因此,您必须首先将其分配给某个变量并使用

类似:

$getName = mysql_real_escape_string($_GET['name']);
$mysql = "SELECT * FROM places WHERE `postal_code` LIKE '%".$getName."%' ";

我希望它能帮助你

使用此

$mysql = "SELECT * FROM table WHERE input LIKE '%".mysql_real_escape_string($_GET['name'])."%' ";