简体   繁体   English

PHP MySQL搜索两列

[英]php MySQL Search on two columns

I'm trying to do a search function using php and MySQL. 我正在尝试使用php和MySQL做搜索功能。 I need to search from two different columns, one for song title and one for artist and user can search using: 我需要从两个不同的列中进行搜索,一列用于歌曲标题,一列用于艺术家,用户可以使用以下方法进行搜索:

1. artist name.
2. song title.
3. both of them (without order)
4. there will be special characters for ex: ' (apostrophe)

This is what I have done but I need a more efficient way to do this. 这是我所做的,但是我需要一种更有效的方法。 I have also thought using similar_text(); 我也曾想过使用similar_text(); in php. 在PHP中。 Can anyone suggest me a better way to do this. 谁能建议我这样做的更好方法。 Thank You 谢谢

table: songs 表: songs

|artist   |    title   |
|----------------------|
|eminem   | not afraid |
|kida     | o'najr     |

My code: 我的代码:

$search_value = $_POST["search_value"];
$query = "select * from `songs` where concat(`title`, `artist`) like '%$search_value%'";

You can simply use sql or statement if you don't want confusion . 如果您不希望混淆,可以只使用sql或statement。

$search_value = $_POST["search_value"];//check user input first than create a query.
$query = "select * from `songs` where `title` like '%$search_value%' or  `artist` like '%$search_value%'";
  1. You should use the SQL OR statement, better than CONCAT one in this case. 在这种情况下,您应该使用SQL OR语句,它比CONCAT更好。

  2. Combined with a space before and after what you search, this should give you the expected result ! 在搜索之前和之后加上空格,这应该为您提供预期的结果! (I mean if you search for 'raid' you will not find 'Eminem - Not Afraid', if you want to find it you have to search for 'afraid' for exemple ; If you want to sort the results by revelance, you will have to create indexes and use them, or use Levenshtein method or something else ...) (我的意思是,如果您搜索“突袭”,您将找不到“阿姆-不害怕”,如果要找到它,则必须搜索“害怕”;例如,如果您想通过启示来对结果进行排序,必须创建索引并使用它们,或者使用Levenshtein方法或其他方法...)

  3. Don't forget to escape your data before using it in sql statements. 在sql语句中使用数据之前,请不要忘记转义数据。

Btw if you want to make it case insesitive you will have to use blabla COLLATE UTF8_GENERAL_CI LIKE %what you search% blabla 顺便说一句,如果您想使其变得不敏感,则必须使用blabla COLLATE UTF8_GENERAL_CI LIKE %what you search% blabla

// if you are using mysql_connect()
$search_value = ' '.mysql_real_escape_string($_POST["search_value"]).' ';
$query = "select * from `songs` where `title` like '%$search_value%' or  `artist` like '%$search_value%'";

// if you are using PDO
$args[':search_value'] = '% '.$_POST["search_value"].' %';

$query = "SELECT * FROM `songs` WHERE `title` LIKE :search_value OR `artist` LIKE :search_value";

$qry = $PDO->prepare($query);
$res = $qry->execute($args);

For a multi-words search you can also use 对于多词搜索,您还可以使用

// for mysql_connect()
$search_value = $_POST["search_value"];
$search_value = explode(' ', $search_value);
foreach($search_value as $k => $v){
    $search_value[$k] = mysql_real_escape_string($v);
}
$search_value = ' '.implode(' % ', $search_value).' ';

// for PDO
$search_value = explode(' ', $_POST["search_value"]);
$search_value = implode(' % ', $search_value);
$args[':search_value'] = '% '.$search_value.' %';

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

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