Searching multiple keywords against multiple columns/tables in a database. PHP + MYSQL

人盡茶涼 提交于 2020-04-12 05:47:51

问题


I am in the process of producing a site search for my website (http://jpdesigns.me/studentnights) and have currently managed to implement searching across multiple tables using a keyword following the answers given in this question - PHP mysql search multiple tables using a keyword

I am currently searching over 3 tables.
- Clubs
- Events
- Music

And i have the following query:

$keystring = $mysqli->real_escape_string($_POST["s"]);
$keystring = strtoupper($keystring);
$key = "%".$keystring."%";

$search_stmt = $mysqli->prepare("SELECT id, name, type AS 'col3', city AS 'col4', 'Club' AS 'table' 
                                 FROM studentnights_clubs 
                                 WHERE UCASE(name) LIKE ? OR UCASE(city) LIKE ?
                                 UNION
                                 SELECT id, name, description AS 'col3', image AS 'col4', 'Event' AS 'table' 
                                 FROM studentnights_events 
                                 WHERE UCASE(name) LIKE ?
                                 UNION
                                 SELECT id, name, '' AS 'col3', '' AS 'col4', 'Genre' AS 'table' 
                                 FROM studentnights_music 
                                 WHERE UCASE(name) LIKE ?
                                 ORDER BY
                                    CASE
                                        WHEN name LIKE ? THEN 1
                                        WHEN name LIKE ? THEN 3
                                        ELSE 2
                                    END
                                LIMIT 10");
$search_stmt->bind_param('ssssss', $key, $key, $key, $key, $key, $key);
$search_stmt->execute();
$search_stmt->store_result();
$search_stmt_num = $search_stmt->num_rows;
$search_stmt->bind_result($id, $name, $col3, $col4, $table);


Note: Blank columns entered in the music table are required to make the query work, union queries require the same number of columns being selected in each table.

Onto the actual question. Whereas by above query search the multiple tables fine with the one keyword i want to expand on this so that multiple keywords can be taken into consideration.

This is easiest to explain by letting you see in real time.

Go to http://jpdesigns.me/studentnights/
Enter 'mint' into the search bar, dont press enter, let ajax do its work.
Notice 2 Clubs Are Displayed, Mint Club (Leeds), Mint Club (London).
Add 'leeds' into the search bar (so 'mint leeds').
Notice both clubs disappear.

What i want is, when 'mint leeds' is typed into the search bar it shows mint club in leeds,
because the 'name' of club is 'mint club' (thus contains the word 'mint')
and its in the 'city' called 'leeds'

Thank you for reading and know that any help would be massively appreiciated.


EDIT: Stament updated so that is parameterised.

来源:https://stackoverflow.com/questions/23428273/searching-multiple-keywords-against-multiple-columns-tables-in-a-database-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!