Search by special characters - php

In our table contains column named as description.
The description column contains following values.
test,%test,test%.
If I give %test as input of search string , then it give "test","%test","test%" as result. But I want only "%test" as result.

% is a special character and needs to be escaped (http://dev.mysql.com/doc/refman/5.0/en/string-literals.html)
Try "\%test"
Snippet:
select description from tablename where decription like '\%test'

Related

Normalize unicode character in sql query

Unicode characters can be encoded in different ways, canonical equivalence and compatibility. We can normalize them to get the same character as it is important for exact string match. My sample code base
$search = Normalizer::normalize($request->get('search'), Normalizer::FORM_C);
I am normalizing the request search keyword in NFC (Canonical Decomposition followed by Canonical Composition). And then I used that normalized string to search in database.
$products = Product::where('title', "LIKE", "%" . $search . "%")->get();
this line generates a sql query
select * from `products` where `title` LIKE %রায়হান% and `products`.`deleted_at` is null
Here রায়হান is my search string. I get two different encoding for the character য়. One is for য় itself that is %E0%A7%9F and one is %E0%A6%AF%E0%A6%BC that is য + ়
My database values may have both the encoding as I didn't normalize them before saving. So when searching I get the values only those matches the normalized search key. Even if I don't normalize the search key, getting only matching encoded values.
Now I know I can normalize the string before saving and then search with the normalized string. But I already have some values that is not normalized. So is there any way to normalize the string when querying the database? Something like
select * from `products` where NormalizedNFC(`title`) LIKE %my_normalized_value_in_php% and `products`.`deleted_at` is null

Deleting part of a string that is saved in MySQL field

I have done my research on the subject and I thought about using replace, but the trouble is if I replaced with nothing, then I will end up with lots of spaces in my database field which will get very messy. So I have come to you guys for help.
I have a field in my database called EQUIPMENT. This is an example of what is in one of the fields
15<>5<>6<>3<>2<>1<>14<>13<>12
Each number is representing the ID of a piece of equipment. How would I go about deleting for example <>6
As I said, I looked at using replace but replacing it with ' ' would leave spaces in the field.
Replace with '' (no space) should not leave a space. You could also use trim, if you don't want any whitespaces.
I m not sure if this is what you want, but before inserting value just use:
echo str_replace("<>", "", "15<>5<>6<>3<>2<>1<>14<>13<>12");
Output: 1556321141312
To delete <>6 use echo str_replace("<>6", "", "15<>5<>6<>3<>2<>1<>14<>13<>12");
Outcome: 15<>5<>3<>2<>1<>14<>13<>12
If you want to do this on mysql database then this could help you: https://stackoverflow.com/a/14586441/5459942
Instead of replacing a "<>number" by an empty string, you can replace a "<>number<>" by "<>".
This is to avoid that other numbers (that contain that number) are also replaced partially.
And to avoid a wrong result when the number is at the start or the end of the string, a '<>' is concatinated to both ends.
Then those '<>' at both ends can be trimmed after the replace.
Example:
select
EQUIPMENT,
TRIM(BOTH '<>' FROM REPLACE(CONCAT('<>',EQUIPMENT,'<>'),CONCAT('<>',6,'<>'),'<>')) as Equipment_without_6
from
(
select '15<>5<>6<>3<>2<>1<>14<>13<>12' as EQUIPMENT
union all select '6<>16<>6<>60<>6'
) q
Returns:
EQUIPMENT Equipment_without_6
------------------------------ ---------------------------
15<>5<>6<>3<>2<>1<>14<>13<>12 15<>5<>3<>2<>1<>14<>13<>12
6<>16<>6<>60<>6 16<>60

SQL Searching for String in DB with Special Character/s

I'm trying to do a search in the database with special characters, specifically string with apostrophe.
For example, I want to search for the string: "Sandy's dog", but I just entered "sandys dog" leaving out the apostrophe. Even though "Sandy's dog" exists in the database, it doesn't seem to show it in the results.
Here's my query:
SELECT * FROM `Table` WHERE `Title` LIKE '%sandys dog%'
I have searched everywhere and I can't seem to find a solution that works.
EDIT
Limitations: the string is user generated
Notes:
- If a user searches for sandy's dog with the apostrophe, it works fine as expected.
- Ultimately I would like to get all possible results, if the table contains both strings with and without apostrophe.
In SQL server, you can use REPLACE:
SELECT *
FROM Table
WHERE REPLACE(Title, '''', '') LIKE '%sandys dog%'
The double-apostrophe inside the string is an escape character, so it finds any apostrophes in the string and replaces them with blank strings.
Please try using escape sequences.
http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
Something like, SELECT * FROM Table WHERE Title LIKE '%sandy\'s dog%'
How about this?
SELECT * FROM Table WHERE Title LIKE '%sandy''s dog%'
or
SELECT * FROM Table WHERE Title LIKE '%sandy_s dog%'
The underscore is a "single character" wildcard.

Get exact match with LIKE - SQL/PHP

Given the text:
"Hello, I'm from Hell",
"Hello, I'm from Ell"
and the following SQL clause:
SELECT * FROM table WHERE text LIKE '%ell%'
I get both the texts above, but I don't want to get both texts, because I was looking for the text
"Ell" and not "Hell"
If anyone knows what I mean, can you help me out?
Thanks in advance!
EDIT:
BETTER EXAMPLE
Like when you want to look for the word 'big' but it can't be part of any other word like 'bigger' or 'biggest'
You could use the MySQL Regex word boundary matching:
SELECT * FROM table WHERE text REGEXP '[[:<:]]ell[[:>:]]'
This matches a string which cosntains the word ell with a word boundary on either side.
Can you search for a space as well?
SELECT *
FROM table
WHERE text LIKE '% ell%'
OR text LIKE 'ell%' // needed if string starts with ell
You can try this:
SELECT * FROM table WHERE text LIKE BINARY '%ell%'
Select the exactly match no case sensitive
SELECT * FROM table
where LOWER(column) like BINARY LOWER('ell')
It works if the text has multi-line.
you can just use REGEXP in the query. have a look a this resource which contains all the detail you need.
syntax is simple as follows
$sql= "select * from post where title REGEXP '$s'";

php mysql LIKE with special characters does not work properly

So i am trying to do a LIKE query and get some results but the text that i pass has some special characters that break the query.
if we assume that the text is something like this:
var test `select` `query`="$newval + "dsadsa$ ? "$test ?
and i also have exactly the same text inside a column as VARCHAR
and then executing the query
SELECT * FROM table WHERE column LIKE '%$text%'
says that there is no rows to return.
EDIT: when i post the data inside the database i simply use mysql real escape string and when i show the text where i click to search i put htmlentities on the text
then i substr it from 0 to 50 and do the search query
You can use mysql_real_escape_string() which will escape any special characters in your string.
Try to avoid writing variables directly into string, it may cause problems (+ it's really not nice):
mysql_query("SELECT * FROM table WHERE column LIKE '%" . $text . "%'");
Of course make sure that the $text variable is really correct (echo $text), characters escaping may cause problems too and of course there can be many other things causing problems (this depends on architecture of your application - where you work with $text).

Categories