0
$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 '.'???

2
  • I no discuss the syntax, but you should really need to understand the bottleneck of using wildcard search in MySQL. Commented Jan 31, 2013 at 7:08
  • Why not use Full-Text search? devzone.zend.com/26/using-mysql-full-text-searching. Of course, it assumes that the table engine is MyISAM. Commented Jan 31, 2013 at 7:29

2 Answers 2

2
$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%'";
}
Sign up to request clarification or add additional context in comments.

9 Comments

If you want to match both .exe and exe then you should put wildcard with type. type LIKE '%$type%'
this is way I know but is not working I am write file all is gut but when I write file.exe it is not working
do you know where is the problem
second condition it is not searching 'OR type LIKE '$type''
use trim(array_pop($parts)) in place of array_pop($parts) that should search file .exe . If you want more options of search then try full text search or solr/lucene
|
1
$where .= "`title` LIKE '%$keyword%' OR type LIKE '%$type%'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.