I am trying to build a search query where suppose if we type a word stem, it should show words starting with stem and whatever follows after that. Right now, my code looks like this:
$sql = "SELECT * FROM `faculty`
WHERE (CONCAT(`firstName`, ' ', `lastName`,`expertise`,`affiliation`)
LIKE '%$textSearch%') ORDER BY lastname asc";
If I search stem in the search bar, it will show unneccessary words like 'System'.
If you’re looking for a search across multiple columns, but each word must begin with the search term you may have to make it a multiple LIKE statement?
$sql = "SELECT * FROM `faculty` WHERE
`firstName` LIKE ‘$textSearch%’ OR `lastName` LIKE ‘$textSearch%’ OR `expertise` LIKE ‘$textSearch%’ OR `affiliation` LIKE ‘$textSearch%’
ORDER BY lastname asc";
Related
I actually want to search the hashtags with this MySQL query.But its not working for me, I don't know why.
Here is my code. My code uses a PHP variable inside
$searchtext = "#truth";
"SELECT id FROM bp_activity WHERE content REGEXP '[[:<:]]'". $searchtext ."'[[:>:]]'"
This is returning empty but one of the rows in the table contains this expression like this:
Unveilling the #truth
I think it should be sorted out.
But when I only use this this it works
"SELECT id FROM bp_activity WHERE content REGEXP '[[:<:]]truth[[:>:]]'"
But it doesn't work when using like this
"SELECT id FROM bp_activity WHERE content REGEXP '[[:<:]]#truth[[:>:]]'"
So, I have defined a function for searching from DB and passing the results to ajax.
function do_search() {
$search=$_POST['dname'];
global $wpdb;
$sql="SELECT * FROM employee WHERE `firstname` LIKE '%{$search}%' OR `lastname` LIKE '%{$search}%' OR `department`LIKE '%{$search}%' OR `phone` LIKE '%{$search}%' OR `job_title` LIKE '%{$search}%' OR `cell_phone` LIKE '%{$search}%' OR `image`LIKE '%{$search}%' OR `email` LIKE '%{$search}%' OR `address` LIKE '%{$search}%' OR `room` LIKE '%{$search}%';";
$result2=$wpdb->get_results($sql);
echo json_encode($result2);
wp_die();
}
But my problem is its working only with one word. I tried explode the $_POST value with " ". and then making foreach and build multiple queries, like $query[i] but i cant get no response?
How do implement that when echoing the result - then it echoes result where every word is compared to those field entries?
Instead of exploding search query and producing tons of conditions, You can use simple "trick":
$search = $_POST['dname'];
$search = str_replace(' ', '%', $search);
$query = "SELECT * FROM yourTable WHERE col1 LIKE '%{$search}%' OR col2 LIKE '%{$search}%' ";
Don't forget to escape input or parametrizing query.
Exploding and multiple query criterias is the only option here.
this is wrong to say that making a $search variable like this will make it multi word searching the same column. It will not. Because the result of spaces replaced with pecentage wild card will be something like %word1%word2% using above code and multiple percentages will make it search BOTH words not a single one, in that particular column, so multiple percentage wildcards are treated in SQL as AND meaning both should meet in order to be selected, whereas I think we are trying to create a multi word query that treats as OR meaning either word1 or word2 exist, the record should be selected (hence requirement for explode and as many likes required as many words user entered), no this trick of replacing spaces with pecentages, will not work in multiple words.
Mysql database contains below kind of values :
'AE01-1056 Ricoh Aficio' OR 'Ricoh AE01-1087 (AE01-1069)' etc
AS if am a normal user i will search the product name in simple text like
AE011056 ... but the result is not found.
i hav tried this query:
$q="SELECT * FROM mytable WHERE (p.product_name LIKE '$name%' OR c.category_name LIKE '$name%' OR pm.name LIKE '$name%')";
What change should i make in my query to get the product , because i have tried LIKE operator & it's not working for me.
Use replace function
$q="SELECT * FROM mytable
WHERE (REPLACE(p.product_name,'-','') LIKE '%$name%'
OR REPLACE(c.category_name,'-','') LIKE '%$name%'
OR REPLACE(pm.name ,'-','') LIKE '%$name%')";
I think there are only two ways:
1. Manipulate search string
If you knwo, users are often search for a code and don't use nessesary hyphens, check the searchstring bevor searching if it follows a given format an insert the hypen if it is missing.
2. replace all hyphens in the where-statement
see http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
Depending on your setup, solution one might be the more performant solution, since you only have to do one instead multiple stringmanipulations.
The script fetches input from user and searches database field, named tags.
tags contains data for eg: hello, hey, how , happy, hell .. on single cell or you may say as a string.
i tried the query below, but it only works for hello and not for how , happy, hell..
$sql ="SELECT * FROM tws where tags='".$name."' or tags LIKE '".$name.",%' or tags LIKE
',%".$name.",%' or tags LIKE '%,".$name."'";
Note: I thoroughly searched google and stack overflow before posting this question but couldn't get it to work.
Use the following query:
$sql = "SELECT * FROM `tws` WHERE `tags` REGEXP (".$name.")";
You have one percent-sign placed wrong (the third LIKE) and MySQL is quite strict: you have to include the spaces between your tags in the SQL:
$sql ="SELECT * FROM tws where tags='".$name."' or tags LIKE '".$name.",%'
or tags LIKE '%, ".$name.",%' or tags LIKE '%, ".$name."'";
should work (remove the space after how, so your data has a fixed format).
PS: Make sure you use mysql(i)_real_escape_string to escape the value $name
Consider using an extra table where you store your tags. e.g. tws_tags, with fields tws_id and tag and a query with a JOIN on that.
You should omit the commas in your query like this:
$sql ="SELECT * FROM `tws` WHERE `tags` RLIKE '%[[:<:]]$name[[:>:]]%'";
[[:<:]] and [[:>:]] are special markers in MySQL for word boundaries. Alse, in double quotes you can use variables without having to concatenate ("blablabla $smth txt" instead of "blablabla " . $smth . " txt")
Use this:
$sql ="SELECT * FROM tws where tags='".$name."' or
tags LIKE '".$name."%' or tags LIKE
'%".$name."%' or tags LIKE '%".$name."'";
Here your query looks like below if name = ABC
$sql ="SELECT * FROM tws where tags='ABC' or tags LIKE 'ABC,%' or tags LIKE
',ABC,%' or tags LIKE '%,ABC'";
You can see that it is looking for ,ABC not only for ABC
I have a table, with not many rows, and neither many columns. I am doing a Full text search on 3 columns.
My code is
$search_input = trim($_GET['s']);
$search = mysql_real_escape_string($search_input);
$search = '+'.str_replace(' ', '* +', $search).'*';
$sql = "SELECT * FROM table WHERE
MATCH(def, pqr, xyz) AGAINST ('$search' IN BOOLEAN MODE)";
$result = mysql_query($sql);
I can correctly search for terms like abcdefgh, which are present as ... abcdefgh ....
But I am receiving empty set with search terms like abc, where in table entry is present something like abc-123, and also terms like abcdefghs. (notice this is plural of above)
Clearly I need to implement partial search, or something like that.
But how do I implement such a search? Any better way to do a entire table search on user input?
Do mention anything I am doing incorrectly.
EDIT : By adding * after each word, now I am able to also search for abcde, but above problems remains.
Do you mean you don't get results for 3 letter combinations? If so, you might be hitting the mysql index length (which is usually set to 3)
More info here - http://dev.mysql.com/doc/refman/5.1/en/fulltext-fine-tuning.html