开发者

php mysql jquery AJAX autocomplete case sensitivity

开发者 https://www.devze.com 2023-02-10 01:02 出处:网络
In my php script, $names = $_GET[\'part\']; $result = mysql_query(\"SELECT * FROMnamestable where names LIKE\'%$names%\' LIMIT 10\");

In my php script,

$names = $_GET['part'];
$result = mysql_query("SELECT * FROM  namestable where names LIKE'%$names%' LIMIT 10");
while ($row = mysql_fetch_assoc($result)) {
        $colors[]=$row['publishers'];
}

checks for matches and works well.

But suppose my table has a n开发者_JAVA技巧ame Alfred, the suggestion will appear only if i type Alfr and not appearing if i type alfr


The example you've provided will work if you're using a case insensitive collation such as utf8_general_ci. (See the MySQL Character Set Support manual section for more information.)

Alternatively, you could simply use the LOWER function as follows:

$names = strtolower(mysql_real_escape_string($_GET['part']));
$result = mysql_query("SELECT * FROM namestable WHERE names LIKE LOWER('%$names%') LIMIT 10");

Incidentally, if you're attempting to catch differences beyond simple case changes, you could also use MySQL's SOUNDEX function to obtain and find a match for strings that sound similar.

Incidentally, you need to use mysql_real_escape_string on your $names variable, or (better still) use prepared statements via the mysqli or PDO interfaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号