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[[:>:]]'"
Related
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";
I have a loop that is producing a string like $sku = MAR-9-870-2-L. I have a database that is a list of "skusearchquery" that often look like skusearchquery = MAR-9. I am trying to do a search for all rows of the database that have a skusearchquery contained inside the string $sku.
I know the code below doesn't work because MAR-9-870-20-L is NOT LIKE MAR-9-870 because MAR-9-870 doesn't contain the longer string, so I'm wondering how I can say: if the row value skusearchquery matches part of the string MAR-9-870-20-L, then select it.
$search = mysqli_query($connect, "SELECT * FROM skusearch WHERE skusearchquery LIKE '%$sku%'");
Please try the LOCATE() function:
$search=mysqli_query($connect,"SELECT * FROM skusearch WHERE LOCATE(`skusearchquery`,'$sku'");
...if this tests positively, you should take tadman's advice and protect your query.
As you said, that your searchquery field can have smaller part of sku then you can try below query to get those results as well
$search = mysqli_query($connect, "SELECT * FROM skusearch WHERE skusearchquery LIKE '%$sku%'" or '$sku' like concat('%',skusearchquery,'%');
Also make sure to protect your queries from sql injection as suggested by #tadman.
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.
Ok so I have a very big table and I need to search a url which have a keyword in it, and I am trying to do it with LIKE but LIKE is working just like this 'foo%' and this checks if the string starts with foo and what I try to get is '%foo%'.
$query = mysqli_query($link,"SELECT * FROM table WHERE url LIKE 'foo%' LIMIT number");
If you can give me an idea it would be perfect. Thanks!
Also I have an index for url and id.
Using LIKE will slow the query down; one idea would be to try LOCATE instead:
$query = mysqli_query($link,"SELECT * FROM table WHERE LOCATE('foo', url) > 0 LIMIT number");
LOCATE will simply find the index of the match else return 0, similar to strpos() in PHP (except strpos() will return false if no match). There is no wildcard matching.
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