-4
 <?php
    include"configration.php";  
?>

 <?php
    $query = $_GET['query'];
    $min_length = 1;
    //echo $query;exit();
    if (strlen($query) >= $min_length) { // if query length is more or equal minimum length then
        //echo "success";exit();
        $query = htmlspecialchars($query);
        $query = mysqli_real_escape_string($conn, $query);
        $sql = "SELECT * FROM table2
                WHERE title LIKE '%".$query."%' order by date DESC";
        $raw_results = mysqli_query($conn, $sql) or die(mysql_error());
        if (mysqli_num_rows($raw_results) > 0) { // if one or more rows are returned do following
            while ($res = mysqli_fetch_array($raw_results)) { ?>
    
              <?php echo $res['title'] ?> // Place where result comes ..
    
            <?php }
        }
    }
    ?>

This is code works fine but search in this way

For Example Title is: you are vary nice boy but lazy

When I search by:

You are vary ............. result shows ..

vary nice boy ............. result shows ..

vary lazy, or boy lazy or vary lazy .. result not shows ..

Plz some one help me in this and how to show searched query in title ..

   <title> Searched Query ...</title>
Community
  • 1
  • 1
Sahil
  • 1
  • 1
  • 2
    beware : you are mixing `mysqli_x` and `mysql_x` functions. you have to use `mysqli_error()`. Also, you should have a look at **prepared statements** which are safer than escaping strings with `mysqli_real_escape_string` – ᴄʀᴏᴢᴇᴛ Aug 31 '18 at 15:04
  • 2
    Use prepared statements, not escape strings. There's also probably no reason to use htmlspecialchars here, that should be used on output, not input.. – Devon Bessemer Aug 31 '18 at 15:06
  • 1
    I don't think this is a duplicate of the question indicated. OP wants to know how to match a specific string pattern using "LIKE". – Simon Aug 31 '18 at 15:09
  • I for one have no idea what you're asking here. – Funk Forty Niner Aug 31 '18 at 15:12
  • 1
    I've re-opened, there are multiple issues at hand here. – Devon Bessemer Aug 31 '18 at 15:12
  • I see what he is trying to do... but its really a complex issue to cover in a simple example based on his code :( – IncredibleHat Aug 31 '18 at 15:13
  • @Devon actually, I was about to include 2 more duplicates for this, as their query failed, outright. I might reclose. – Funk Forty Niner Aug 31 '18 at 15:13
  • yeah, that's fine but i don't think the dup I chose represented the main issue of the question – Devon Bessemer Aug 31 '18 at 15:14
  • 3
    The way to do this is to `explode` the string by a regex (word boundaries), and then construct a series of `LIKE` clauses for each separate word. I've done it before, but @FunkFortyNiner is right - a big ask for what has been provided as far as code goes. – random_user_name Aug 31 '18 at 15:14
  • @Devon it's part of it. If the query failed, the `mysql_error()` would not show them real reason why it failed, hence the "mixing of apis" fits the bill for me. – Funk Forty Niner Aug 31 '18 at 15:15
  • @FunkFortyNiner but his query really isn't failing ... its just not returning the rows he wants when searching by two split words instead of a string of matching words. Bit of a difference there ;) – IncredibleHat Aug 31 '18 at 15:16
  • Edit ping: @IncredibleHat --- (original) Meh, I'll just vote to close this as a generic and won't be bothered. – Funk Forty Niner Aug 31 '18 at 15:16
  • 1
    @IncredibleHat Too broad and unclear, IMHO. – Funk Forty Niner Aug 31 '18 at 15:19

2 Answers2

0

When you search title LIKE "%vary lazy%", you will get records that contain the string "vary lazy" preceeded and followed by any other or no character sequences. If you want to match strings that contain the words - I should better say, the character sequences - "vary" and "lazy" in that specific order you should use:

title LIKE "%vary%lazy%"

However, this will also match "varylazy", "varying lazytown characters".

Assuming you generally intend to use queries as you mentioned, i.e. each word is separated by a space character and you want to see if those words appear in a text in specifically that order, you could write something like this:

$query = $_GET["query"];
$query = '%'.str_replace(' ', '%', $query).'%';

//... MySQL stuff

Please be aware that the code above is very specific to your needs. I wouldn't use it as a general purpose approach for processing query strings, e.g. having multiple spaces between words would result in multiple consequent % in your SQL query - I'm not even sure if that is allowed. However, under the constraints described, this code should work just fine.

Simon
  • 9,255
  • 4
  • 37
  • 54
  • WHERE title LIKE '%".$query."%' what to change .. means how to write according to the way you said.. – Sahil Sep 02 '18 at 21:34
0

LIKE '%boy lazy%' will show the Of the cases where anything can be before boy lazy and anything can be after boy lazy, but boy lazy will be together.

In your case, one approach can be, you can explode your $query, and then use multiple LIKE queries to create sql query. Example:

<?php
//$conn = mysqli_connect("localhost","your user","your pass","db");
$query = $_GET['query'];
$min_length = 1;
//echo $query;exit();
if (strlen($query) >= $min_length) { // if query length is more or equal minimum length then
    //echo "success";exit();
    $query = htmlspecialchars($query);
    $query = mysqli_real_escape_string($conn, $query);
    $searchKeys = explode(' ',$query);
    $sql = "SELECT * from table2 where title ";
    foreach ($searchKeys as $key) {
      $sql.= "LIKE '%".$key."%' AND title ";
    }
    $sql = substr($sql, 0, -10);
    //$sql.="ORDER BY date DESC;";
    $raw_results = mysqli_query($conn, $sql) or die(mysql_error());
    if (mysqli_num_rows($raw_results) > 0) { // if one or more rows are returned do following
        while ($res = mysqli_fetch_assoc($raw_results)) {
            echo $res['title']."\n";
         }
    }
}
Anshu Kumar
  • 807
  • 1
  • 8
  • 27
  • you have complete code ? .. i tried but result not shows .. if you have time then Plz give the complete code... lot of errors comes when i write code in this format . – Sahil Sep 02 '18 at 21:32
  • @Sahil I have editted the my answer above with the full code. If u still have problems you may comment. – Anshu Kumar Sep 03 '18 at 03:44
  • i tried this code but where to show results and how... i tried that code you write but no result shows .. i have edited the question code that how i display results.. there are 9 fields all other fields comes according to searched title .. i display results like this and other field also like this .. – Sahil Sep 05 '18 at 06:48