簡體   English   中英

PHP的MySQL從2列搜索

[英]php mysql search from 2 columns

$where = "";
$keywords =preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);

foreach($keywords as $key=>$keyword) {
    $where .= "`title` LIKE '%$keyword%'";
    if ($key != ($total_keywords - 1)) {
        $where .= " AND ";
    }
}

    $results = "SELECT `id`, `username`, `title`, LEFT(`description`, 90) as `description`, `file`, `code`, `type`, `size`, `date` FROM `files` WHERE $where ORDER BY id DESC LIMIT $start, $per_page";


  $where .= "`title` LIKE '%$keyword%'"; // this case I am searching only in column `title` I want to search too in column `type` with sign '.' I don't know how to include '.' in search result for `type`

示例搜索file.exe,並使用此條件file.exe獲取所有結果。 或只搜索.exe即可獲得所有type .exe $ where。=“ titletype LIKE'%$ keyword%'”“的結果; //我這樣做是沒有結果的$ where。=” titletype LIKE'% $ keyword%'“; //我這樣做是沒有結果$ where。=” title LIKE'%$ keyword%'並type LIKE'%$ keyword%'“; //我這樣做是沒有結果有人知道如何獲得標題類型為“。”的結果?

$where .= "`title` LIKE '%$keyword%' OR type LIKE '$type'";

更新的代碼

$pos = strpos($keyword, '.');
if($pos > 0) {
    $parts = explode('.', $keyword);
    $type = array_pop($parts);
    $file = array_pop($parts);
    $where .= "`title` LIKE '%$file%' AND type LIKE '%$type%'";
}
else {
$where .= "`title` LIKE '%$keyword%' OR type LIKE '%$type%'";
}
$where .= "`title` LIKE '%$keyword%' OR type LIKE '%$type%'";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM