I have a table for places on database it contains two rows for title and description
I made a search by PHP and MYSQL like that
$select_place = $mysqli->query("SELECT * FROM places where title LIKE '%$searchname%' or description LIKE '%$searchname%'");
$num_place = $select_place->num_rows;
while ($rows_place = $select_place->fetch_array(MYSQL_ASSOC)){
$id_place = $rows_place ['id'];
$title_place = $rows_place ['title'];
$description_place = $rows_place ['description'];
echo "<p>{$title_place}</p><br>";
}
It works well, But, for example, if you search for the word tower if written in wrong way like twer or towr it doesn't work How can i make it more intelligent?
One good option is to include MySQL SOUNDEX() function:
SELECT
*
FROM
places
WHERE
title LIKE '%$searchname%'
OR description LIKE '%$searchname%'
OR SOUNDEX(title) = SOUNDEX('$searchname')
OR SOUNDEX(description) = SOUNDEX('$searchname')
This will match both towr and twer from your example.
More can be found here:
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_soundex
Note that:
This function, as currently implemented, is intended to work well with
strings that are in the English language only. Strings in other
languages may not produce reliable results.
i found very useful way to do it
here it is
$searchname = stringsafe($_GET['search']);
$searchname2 = explode(" ", $searchname);
$query = "SELECT * FROM places WHERE ";
foreach ($searchname2 as $each) {
$i++;
if($i == 1){
$query .= " title LIKE '%$each%'
OR description LIKE '%$each%'
OR SOUNDEX(title) = SOUNDEX('$each')
OR SOUNDEX(description) = SOUNDEX('$each')";
}else{
$query .= " OR title LIKE '%$each%'
OR description LIKE '%$each%'
OR SOUNDEX(title) = SOUNDEX('$each')
OR SOUNDEX(description) = SOUNDEX('$each')";
}
}
$select_place = $mysqli->query("$query limit 20");
$num_place = $select_place->num_rows;
if($num_place == 0){
echo "<div class='message-no'>No results</div>";
}
else{
while ($rows_place = $select_place->fetch_array(MYSQL_ASSOC)){
$id_place = $rows_place ['id'];
$title_place = $rows_place ['title'];
$description_place = $rows_place ['description'];
echo $title_place."<br>";
}
Related
I want to create code to filter my catagory and tag, I'm using select box to filter, my code like this:
if ($filter2) {
$addedCondition = " AND (nama_kat LIKE '%$filter2%' OR nama_tag LIKE '%$filter2%') ";
}
$query = $db->prepare("SELECT * FROM konten
WHERE (nama_kat LIKE '%$filter1%'
OR nama_tag LIKE '%$filter2%') ".$addedCondition."
ORDER BY 'date' DESC");
work if I'm only filter one word, but once I filter with two words it's not working, my web displaying all articles.
you can try filter here http://stanime.pe.hu/
You have to explode your string into words and use OR.
$addedCondition = '';
$filter2 = explode(' ',$filter2);
foreach($filter2 as $word)
{
$addedCondition .= " OR (nama_kat LIKE '%$word%' OR nama_tag LIKE '%$word%') ";
}
I'm having some problem searching full text in MySQL. The code below only works on my localhost, but on my hosted server it throws an error:
"Can't find FULLTEXT index matching the column list"
I've had various attempts, also tried to alter my table but the error still remains. Note that the code is working well on my localhost, and printing out actual results, however it fails on running it on live server.
This is my code:
function relatedArticle($item, $category){
global $con;
$keywords = trim(removePrepositions($item));
$keywords = str_replace(',','', $keywords);
$keywords = preg_replace('/\s+/',' ',$keywords);
$keyword_tokens = explode(' ', $keywords);
$query_result = implode('+', $keyword_tokens);
$construct = '';
$construct = $query_result;
$query = "SELECT DISTINCT * FROM apps WHERE Match(title) AGAINST ('$construct' IN NATURAL LANGUAGE MODE) and active = 'active' and Type = '$category' limit 4";
$query_result = $con->query($query) or trigger_error($con->error."[$query]");
echo "<div id='Big_container_rel'>";
echo "<div id='container_rel'>";
while($row = $query_result->fetch_array()) {
echo "<div id='container1_rel'>";
$AdTitle = pre_replace($row['title']);
$type = strtolower(pre_replace($row['Type']));
echo "<a href='/".$type."/".$row['appId']."/".$AdTitle."/'><img id='imgMobi' alt=".$row['title']." src='uploads/".$row['image']."'/></a>";
echo "<br/><h4><a href='/".$type."/".$row['appId']."/".$AdTitle."/'>".$row['title']."</a></h4>";
echo "</div>";
}
echo "</div>";
echo "</div>";
}
My $construct is returning text joined with '+' sign. For example , find+fulltext+on+stack
Please help avoid this problem; I tried several solutions from StackOverflow, but they haven't worked for me so please don't mark it as duplicate.
I am trying to create a search Function on my website using PHP and SQL.
I am trying to make it so that when i search for a keyword in it displays items matching the key word
This is my Database
If i was to search "rice chicken" it would return both Davida and roys, i would like it to only return Davids as it matches both words and not roys as roys only matches rice
Thos is my current code.
$search = $_GET['query'];
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("axamenu");
$query = mysql_query("SELECT * FROM menu WHERE CONTAINS(items,'$.search')");
if(mysql_num_rows($query) >= 1) {
while($a = mysql_fetch_array($query)) {
echo "<a href='".$a['profileurl']."'>".$a['restaurant']."</a><p>".$a['menutype']."</p><hr/>";
}
} else {
echo "Oh no! Nothing was found.";
}
You could make your query in this way
$where = '';
$string = "thi sdas asd";
$search = explode(" ",$string);
if(sizeof($search)>1)
{
$where = "1=1";
foreach($search=>$key as $val){
$where .= "or items like %".$val."%";
}
}
else
{
$where = "items like %".$search[0]."%"
}
$query = mysql_query("SELECT * FROM menu WHERE ".$where);
//rest of your code goes here
Note: Stop using deprecated and insecure mysql_*-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7. Use MySQLi or PDO instead
If you only want davids result as "rice chicken",
you can split your string as rice and chicken and query as
SELECT * from menu WHERE items REGEXP 'rice' AND items REGEXP 'chicken'
you will get only one result
NOTE: using REGEXP can slow down result for bulk data retrieving
$search = $_GET['query'];
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("axamenu");
$query = mysql_query("SELECT * FROM menu WHERE
items LIKE '%".$search."%'");
if(mysql_num_rows($query) >= 1)
{
while($a = mysql_fetch_array($query))
{
echo "<a href='".$a['profileurl']."'>".$a['restaurant']."</a><p>".$a['menutype']."</p><hr/>";
}
}
else
{
echo "Oh no! Nothing was found.";
}
Try with this and let us if that works.
select * from table_name where items like 'rice%' or items like 'chicken%';
CODE :
$nerd_result = mysql_query("select * from nerd_profile where nerd_reg_no = '$reg_no'");
$nerd_data = mysql_fetch_array($nerd_result);
$tags = array();
$tags = explode(",",$nerd_data['nerd_interests']);
for($i = 0; $i < sizeof($tags)-1; $i++)
{
if($i != sizeof($tags)-2)
{
$sub_query = $sub_query."`tags` like %".$tags[$i]."% or ";
}
else
{
$sub_query = $sub_query."`tags` like %".$tags[$i]."% ";
}
}
$proper_query = "select * from `qas_posts` where ".$sub_query." and `post_date` like '%$today%'";
$result = mysql_query($proper_query);
while($each_qas = mysql_fetch_array($result))
Description :
I am adding the like clause along with php variable in a string and concatenating it with the further variables with like clause to come. In the end when I echo I get the perfect query that I want but
mysql_fetch_array()
does not accept that generated query rather if I hard code it , it works perfect what am I doing wrong ?? can I do that ??
When doing string comparisons in mysql you need to make sure you have quotes around your comparison value.
$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' or ";
and
$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' ";
As I am learning PHP, naturally, I decided to create a search feature on my webpage. However I wanted to make mine more unique, so rather than using just a simple html input field as the 'search' field, I created two html select tags which allow the user to select two options and search based upon that. I managed to get the php to generate the search query, however it wasn't the sql query I wanted. My php code managed to generate a query hat looked like this: .com/results.php?option1=london&option2=car whereas ideally I want it to generate something like this: .com/results.php?combinedoptions=london+car
I've researched thoroughly into this and I hate to ask, what may be, a very simple question on this site.
$input = $_GET['input'];
$topic = $_GET['topic'];
$location = $_GET['location'];
$combined = $input . $topic . '' . $location;
$terms = explode(" ", $combined);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%'";
else
$query .= "OR keywords LIKE '%$each%'";
}
You would just split the incoming string. Here's a piece of code:
<?php
$combinedoptions = 'london+car';
$array = explode("+", $combinedoptions);
if (sizeof($array) != 2) { /*problem here*/ echo 'bad parameters'; return; }
$option1 = $array[0];
$option2 = $array[1];
?>
Just using the explode() method. Compiled code.