Php 使用MySQL获取查询中的语法错误
我在执行sql查询时遇到以下错误Php 使用MySQL获取查询中的语法错误,php,mysql,Php,Mysql,我在执行sql查询时遇到以下错误 #1064-您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解在第1行使用“s%”ORDER BY gallery_id DESC“的正确语法 我在下面解释我的问题 $searchKey="Celli's" $keyword = '%'.$searchKey.'%'; $query = "SELECT * from db_gallery WHERE description LIKE '" . $keyword . "' OR
#1064-您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解在第1行使用“s%”ORDER BY gallery_id DESC“的正确语法
我在下面解释我的问题
$searchKey="Celli's"
$keyword = '%'.$searchKey.'%';
$query = "SELECT * from db_gallery WHERE description LIKE '" . $keyword . "'
ORDER BY gallery_id DESC ";
这里我需要搜索值,但当的
带有任何关键字时,它就会抛出错误。请帮帮我。您必须转义“字符,如:
searchKey = 'something'
searchKey = searchKey.replace("'", "\\'");
现在在查询中使用searchKey
或者使用一些查询转义字符串函数来转义这些字符。在$searchKey
中包含一个,
,因此在您的查询中实际上有三个,
不对应
你应该先逃跑(两人都没事):
请参阅:更改$searchKey=“Celli's”
使用$searchKey=“Celli”(缺少反斜杠)请尝试以下查询
$keyword = '%Celli''s%';
"SELECT * from db_gallery WHERE description LIKE ".$keyword." ORDER BY gallery_id DESC";
$searchKey中包含一个“search”,因此您需要对其进行转义,请使用mysqli转义字符串:
注意:mysqli\u real\u escape\u string
要求第一个参数作为连接
$keyword="Celli's"
$con=mysqli_connect("localhost","root","root","test");//procedural
$searchKey = mysqli_real_escape_string($con, $keyword);
echo $searchKey;// use this in sql
如果使用的是对象类型,则:
$con=new mysqli("localhost","root","root","test");//procedural
$searchKey = $mysqli->real_escape_string($keyword);
echo $searchKey;// use this in sql
简单的错误请尝试此操作。在搜索字符串中有”
,因此当您尝试执行查询时,它将返回语法错误。因为这会打断你的查询。因此,在关键字中使用mysql\u real\u escape\u string
。搜索关键字值可能类似于celli或celli的
。@subhra如果您使用其他方式连接数据库并发送查询,请使用其字符串转义方法。
$con=new mysqli("localhost","root","root","test");//procedural
$searchKey = $mysqli->real_escape_string($keyword);
echo $searchKey;// use this in sql
$searchKey="Celli\'s"
$keyword = '%'.$searchKey.'%';
$query = "SELECT * from db_gallery WHERE description LIKE '" . $keyword . "'
ORDER BY gallery_id DESC ";