Search code examples
phpmysqlsearch-engine

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`

Example search for file.exe and get all results with this condition file.exe. Or somebode search only .exe get all results with type .exe $where .= "title, type LIKE '%$keyword%'";//I did it like this no result $where .= "title AND type LIKE '%$keyword%'";//I did it like this no result $where .= "title LIKE '%$keyword%' AND type LIKE '%$keyword%'";//I did it like this no result Someone knows how I can get it my results with title type with sign '.'???


Solution

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

    Updated Code

    $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%'";
    }