1 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
您的代碼對(duì)SQL 注入相關(guān)的攻擊是開(kāi)放的。甚至real_escape_string無(wú)法完全保護(hù)它。請(qǐng)學(xué)習(xí)改用Prepared Statements。
現(xiàn)在,除了上述建議之外,還有兩個(gè)可能的進(jìn)一步修復(fù):
修復(fù) #1您用于將輸入字符串標(biāo)記為 FTS 單詞的 php 代碼不足。前段時(shí)間,我確實(shí)創(chuàng)建了一個(gè)函數(shù)來(lái)以更強(qiáng)大的方式處理這個(gè)需求。您可以改用以下內(nèi)容:
/**
* Method to take an input string and tokenize it into an array of words for Full Text Searching (FTS).
* This method is used when an input string can be made up of multiple words (let's say, separated by space characters),
* and we need to use different Boolean operators on each of the words. The tokenizing process is similar to extraction
* of words by FTS parser in MySQL. The operators used for matching in Boolean condition are removed from the input $phrase.
* These characters as of latest version of MySQL (8+) are: +-><()~*:""&|
* We can also execute the following query to get updated list: show variables like 'ft_boolean_syntax';
* Afterwards, the modified string is split into individual words considering either space, comma, and, period (.) characters.
* Details at: https://dev.mysql.com/doc/refman/8.0/en/fulltext-natural-language.html
* @param string $phrase Input statement/phrase consisting of words
* @return array Tokenized words
* @author Madhur, 2019
*/
function tokenizeStringIntoFTSWords(string $phrase) : array {
$phrase_mod = trim(preg_replace('/[><()~*:"&|+-]/', '', trim($phrase)));
return preg_split('/[\s,.]/', $phrase_mod, null, PREG_SPLIT_NO_EMPTY);
}
修復(fù) #2似乎您正在嘗試通過(guò)按以下順序給予優(yōu)先級(jí)來(lái)對(duì)搜索進(jìn)行排名:
文本中的所有單詞>第一個(gè)單詞 AND 其余兩個(gè)單詞>中的任何一個(gè) 至少三個(gè)單詞中的任何一個(gè)。
但是,如果您閱讀全文搜索文檔,您可以使用相關(guān)性進(jìn)行排序MATCH(),因?yàn)樗€返回相關(guān)性分?jǐn)?shù)。
當(dāng)MATCH()在WHERE子句中使用時(shí),返回的行將自動(dòng)以最高相關(guān)性?xún)?yōu)先排序(不幸的是,這僅適用于 NATURAL 模式,而不適用于 BOOLEAN 模式)。相關(guān)性值是非負(fù)浮點(diǎn)數(shù)。零相關(guān)性意味著沒(méi)有相似性。相關(guān)性是根據(jù)行(文檔)中的單詞數(shù)、行中唯一單詞的數(shù)量、集合中的單詞總數(shù)以及包含特定單詞的行數(shù)來(lái)計(jì)算的。
所以基本上,文本中的所有單詞已經(jīng)比這三個(gè)單詞中的任何一個(gè)都具有更高的相關(guān)性?,F(xiàn)在,如果你需要給第一個(gè)詞更高的優(yōu)先級(jí),你只需要>在第一個(gè)詞上使用操作符。因此,您只需要以下單個(gè)查詢(xún):
SELECT * FROM movie
WHERE
MATCH(name)
AGAINST('>:first_word :second_word :third_word ..and so on)' IN BOOLEAN MODE)
ORDER BY
MATCH(name)
AGAINST('>:first_word :second_word :third_word ..and so on)' IN BOOLEAN MODE)
DESC
LIMIT 20
- 1 回答
- 0 關(guān)注
- 165 瀏覽
添加回答
舉報(bào)