简体   繁体   English

PHP变量和MySQL LIKE查询无法正常工作

[英]PHP variable and MySQL LIKE query is not working

I have the following code: 我有以下代码:

$surname=$_POST['surname'];
$sql2="SELECT * FROM andriana WHERE surname LIKE '$surname%'";
if (!mysql_query($sql2,$con)){
die('Error: ' . mysql_error());
}
$result2 = mysql_query($sql2);

echo "<table>";
while ($data = mysql_fetch_array($result2)) {
    echo "<tr>";
    echo "<td style='width:100px;height:40px'>".$data['name']."</td>";
    echo "<td style='width:100px;height:40px'>".$data['surname']."</td>";
    echo "<td style='width:100px;height:40px'>".$data['checkIN']."</td>";
    echo "</tr>";
}
echo "</table><br><br>";

and let's say the following records in my table: 让我们说下表中的以下记录:

- Surname -
Greyjoy
Lannister
Stark

What happens is that if I won't type the full surname, it throws error that that surname doesn't exist. 会发生的是,如果我不输入完整的姓氏,则会抛出该姓氏不存在的错误。 As a result the LIKE "%" is not working. 结果,LIKE“%”无法正常工作。

I have tried LIKE '".$surname."$' or LIKE '{$surname}%', but nothing happens too. 我试过LIKE'“。$ surname。”$'或LIKE'{$ surname}%',但也没有任何反应。

I searched here in Stack a lot, and it seems that the above tryouts should be working. 我在Stack中搜索了很多,似乎上面的试用版应该正常工作。

What am I missing? 我错过了什么?

  • post-comments-editing - 评论后编辑 -

To be more understood, I am sure that the variable contains the actual surname as a string, because if I type the whole surname, my application works normally. 为了更加明白,我确信变量包含实际的姓氏作为字符串,因为如果我输入整个姓氏,我的应用程序正常工作。 However, if I type the first 3 letters (or 4...) the application returns my homemade message that the surname typed is wrong. 但是,如果我键入前3个字母(或4 ...),应用程序将返回我自己的消息,即输入的姓氏是错误的。

Also, to go over the problem with case sensitive, my testing is done with a surname which has only small characters. 另外,为了解决区分大小写的问题,我的测试是使用只有小字符的姓氏来完成的。

Thank you all for your effort, still havinf the issue! 谢谢大家的努力,仍然对这个问题感到满意!

Make sure surname has a value and that you are passing one to it. 确保姓氏有一个值,并且你要传递一个。 I recommend doing a var dump 我建议做一个var转储

$surname=$_POST['surname'];
var_dump($surname);

That will show you the values of what $surname is equal to, if it is nothing, then that is why your query is not working. 这将显示$ surname等于的值,如果它什么都不是,那么这就是你的查询不起作用的原因。

我猜它会以任何方式工作,但试试这个:

"SELECT * FROM andriana WHERE surname LIKE '" . $surname . "%'";

You have two definite problems and one potential problem: 你有两个明确的问题和一个潜在的问题:

First, you aren't using bind variables. 首先,您没有使用绑定变量。 This opens up your script to an SQL injection attack, which is an extremely common and preventable security error. 这会打开SQL注入攻击的脚本,这是一个非常常见且可预防的安全错误。 Replace your SQL script with: 将您的SQL脚本替换为:

$sql2 = "SELECT * FROM andriana WHERE surname LIKE '%?%'";

Then prepare() your statement, binding the variable you want, and execute() it. 然后prepare()你的语句,绑定你想要的变量,并execute()它。 See http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php for more discussion. 有关更多讨论,请参见http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

Second, the % wildcard stands for "any characters", but it is positional, which means you should include it at the beginning of your LIKE argument, as above (" %?% "). 其次, %通配符代表“任何字符”,但它是位置的,这意味着你应该将它包含在LIKE参数的开头,如上所述(“ %?% ”)。

Finally, a potential issue: LIKE is not always case insensitive. 最后,一个潜在的问题:LIKE并不总是不区分大小写。 I think mySQL does case-insensitive LIKEs, but there may be a configuration there that you should set. 认为 mySQL会出现不区分大小写的LIKE,但是可能存在一个应该设置的配置。 When in doubt, either use an ILIKE or manually force a case-insensitive comparison by lowercasing both sides of your comparison. 如有疑问,请使用ILIKE或手动强制进行不区分大小写的比较,方法是降低比较的两侧。

Put the wildcard at the beginning as well as the end: $sql2="SELECT * FROM andriana WHERE surname LIKE '%$surname%'"; 将通配符放在开头和结尾: $sql2="SELECT * FROM andriana WHERE surname LIKE '%$surname%'"; .

I'm a complete idiot. 我是一个完全白痴。 Guyz you were perfect, actually the query with "LIKE '$surname%'" works fine. Guyz你很完美,实际上用“LIKE'$ surname%'”的查询工作正常。

My problem is that before that, I was having a check control and I didn't check for LIKE but for the variable itself. 我的问题是,在此之前,我有一个检查控件,我没有检查LIKE但是检查变量本身。

Please accept my dumpness, and thank you again for your time! 请接受我的倾销,再次感谢您的时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM