mysql真实转义字符串与LIKE不获取任何结果

发布于 2024-12-11 15:03:41 字数 699 浏览 2 评论 0原文

我有一个 mysql 查询,

mysql_query("SELECT name,symbol FROM scode WHERE change='$change' 
AND product='$product' AND series='$typeo' 
AND (name LIKE '%$check%' OR symbol LIKE '%$check%') LIMIT 5");

它工作得很好,但是如果我尝试使用 mysql 字符串使用相同的查询,那么查询不会返回任何结果。 我尝试过这样

$query= sprintf("SELECT name,symbol FROM `scode` WHERE change='%s' 
AND product='%s' AND series='%s' AND (name LIKE '%s' OR symbol LIKE '%s') 
LIMIT 5",
mysql_real_escape_string($change),
mysql_real_escape_string($product),
mysql_real_escape_string($typeo),
mysql_real_escape_string($check),
mysql_real_escape_string($check));

$fetch= mysql_query($query);

如何才能使查询有效?有人可以帮助我吗?谢谢。

I have a mysql query

mysql_query("SELECT name,symbol FROM scode WHERE change='$change' 
AND product='$product' AND series='$typeo' 
AND (name LIKE '%$check%' OR symbol LIKE '%$check%') LIMIT 5");

It works Perfectly but If I try to use the same query using mysql string then query is not returning any result.
i tried like this

$query= sprintf("SELECT name,symbol FROM `scode` WHERE change='%s' 
AND product='%s' AND series='%s' AND (name LIKE '%s' OR symbol LIKE '%s') 
LIMIT 5",
mysql_real_escape_string($change),
mysql_real_escape_string($product),
mysql_real_escape_string($typeo),
mysql_real_escape_string($check),
mysql_real_escape_string($check));

$fetch= mysql_query($query);

How can I make the query that will work? Can some help me? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒绊 2024-12-18 15:03:41

嗯,不,这是行不通的,因为在第一个中你做了一个LIKE %term%,在第二个你做了一个LIKE term。尝试添加 %% ,如下所示:

$query= sprintf("SELECT name,symbol FROM `scode` WHERE change='%s' AND product='%s' AND series='%s' AND (name LIKE '%%%s%%' OR symbol LIKE '%%%s%%') LIMIT 5",mysql_real_escape_string($change),mysql_real_escape_string($product),mysql_real_escape_string($typeo),mysql_real_escape_string($check),mysql_real_escape_string($check));

$fetch= mysql_query($query);

Well no it wouldn't work, because in the first you do a LIKE %term%, in the second you do a LIKE term. Try adding %% around, like this:

$query= sprintf("SELECT name,symbol FROM `scode` WHERE change='%s' AND product='%s' AND series='%s' AND (name LIKE '%%%s%%' OR symbol LIKE '%%%s%%') LIMIT 5",mysql_real_escape_string($change),mysql_real_escape_string($product),mysql_real_escape_string($typeo),mysql_real_escape_string($check),mysql_real_escape_string($check));

$fetch= mysql_query($query);
好久不见√ 2024-12-18 15:03:41

将其添加到 mysql_query 的末尾,它将显示错误消息是什么

  $fetch= mysql_query($query) or die(mysql_error());

Add this to the end of your mysql_query and it will show you what the error message is

  $fetch= mysql_query($query) or die(mysql_error());
伏妖词 2024-12-18 15:03:41

我查了一下,发现一个问题。看起来 mysql 试图将“change”列解析为关键字。尝试将“更改”放入``。

I have checked it and found one issue. Looks like mysql trying to parse "change" column as a keyword. Try to put "change" in ``.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文