user3732711 user3732711 - 6 months ago 10
MySQL Question

search entire table through a term

I have a following table, on which search is performed. when i type "first title" in search bar i get the result. But i wish that if i type just the term "first" then all the rows wherever "first" term is present should get displayed in result. In the given case, first, second and fourth row should get dispalyed as a result. can anyone tell how to do so

table

id title value
1 first title Robert
2 second Max first value
3 third Dean
4 first value Sam


Code for search

<script>
$(function() {
$(".search").on("submit", function() {
var value = $("#search", this).val();

$.post("search.php", {
value: value
}, function(data) {
$("#actionresult").html(data);
});
return false;
});
});
</script>

<form class="search">
<input type="text" name="term" id="search">
<button type="submit" value="submit" name="submit"> </button>
</form>


search.php

$value=$_REQUEST['value'];
$sqlblog="SELECT * from `tablename` where title='".$value."'";
$resultblog = mysqli_query($con, $sqlblog);
if(mysqli_num_rows($resultblog)>0)
{
while($rowblog = mysqli_fetch_assoc($resultblog))
{
//display value
}
}

Answer

As you have said in the question that first, second and fourth row should get displayed, you will have to use LIKE clause along with multiple WHERE using OR. Check following query:

    $value=$_REQUEST['value'];
    $sqlblog="SELECT * from `tablename` where title like '%".$value."%' 
              or value like '%".$value."%' ";
    $resultblog = mysqli_query($con, $sqlblog);
    if(mysqli_num_rows($resultblog)>0)
        {
            while($rowblog = mysqli_fetch_assoc($resultblog))
                {
                    //display value
                }
        }
Comments