SQL Searching for String in DB with Special Character/s - php

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.

Related

Search HTML using LIKE Operator in Database MySQL

I have stored some html in my database, for example:
ID | Data
1 | <a href=\"link\" class=\"someclass\" id=\"id_10923074\"><h3 class=\"class1 class2\"><\/h3><br \/><div class=\"clearfix\"><\/div><\/a>
2 | <a href=\"lin2\" class=\"someclass\" id=\"id_10923075\"><h3 class=\"class1 class2\">some text<\/h3><br \/><div class=\"clearfix\"><\/div><\/a>
Now, I would like to query an invalid records which doesn't contain text in the h3, which is row 1.
I have tried many queries, some are bellow:
SELECT `mytable`.* FROM `mytable` WHERE (Data LIKE '%<h3 class=\"class1 class2\"><\/h3>%')
SELECT `mytable`.* FROM `mytable` WHERE (Data LIKE '%h3 class="class1 class2"></h3%')
SELECT `mytable`.* FROM `mytable` WHERE (Data LIKE '"%class1 class2%"')
SELECT `mytable`.* FROM `mytable` WHERE (Data LIKE '%<h3 class=\"class1 class2\">%')
What am I missing here? I have been checking many questions here but cannot find any solution.
Thank you.
Your code is working as expected. See this sqlfiddle:
SELECT * FROM `mytable` WHERE (Data LIKE '%<h3 class=\"class1 class2\"><\/h3>%')
Your issue most likely lies with your quoting. Ensure you are escaping backslashes (\) and quotes (") properly in your PHP code.
Found the solution here:
So the trick is to double escape ONLY the backslash, for string escapes only a single escape is needed.
For example
The single quote ' only needs escaping once LIKE '%\'%'
But to query backslash \ you need to double escape to LIKE '%\\\\%'
If you wanted to query backslash+singlequote \' then LIKE '%\\\\\'%' (with 5 backslashes)
Explanation Source excerpt:
Because MySQL uses C escape syntax in strings (for example, “\n” to
represent a newline character), you must double any “\” that you use
in LIKE strings. For example, to search for “\n”, specify it as “\n”.
To search for “\”, specify it as “\”; this is because the backslashes
are stripped once by the parser and again when the pattern match is
made, leaving a single backslash to be matched against.
Correct Query:
SELECT * FROM `mytable` WHERE (Data LIKE '%<h3 class=\\\\"class1 class2\\\\"><\\\\/h3>%')
To find rows where Data has at least one <h3> (with or without any attributes such as class=) with no text before </h3>:
WHERE Data REGEXP '<h3[^>]*></h3>'

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'";

MySQL search for value containing spaces returns empty result (no rows)

I have a MySQL database with a column containing part numbers. Some of the part numbers contain spaces:
3864205010  J
When I query the database or search for the part in phpMyAdmin no results are returned.
Yet when I delete the 2 spaces and then type them again, the search returns a result.
This query does not return a result:
SELECT *
FROM `parts`
WHERE `part_no` LIKE '3864205010  K'
This query returns the result:
SELECT *
FROM `parts`
WHERE `part_no` LIKE '3864205010 K'
They look the same but in the second query I have deleted the 2 spaces before "K" and typed the spaces again.
If you can use wildcard instead of spaces:
SELECT *
FROM `parts`
WHERE `part_no` LIKE '3864205010%K'
This is probably not a space but a HTAB (ascii code 9) or even a line feed/carriage return (10 and 13). Copy paste in a good text editor, you'll see what it really is.
Now, regarding to your wonder about why it doesn't work even if it does look like a space, this is because every single character we see is interpreted by the engine (notepad, phpmyadmin, firefox... any software with text rendering)
What actually happens is that when the engine finds an ascii code, it transforms it into a visible character. The CHAR(9) for example is often transformed into a 'big space' usually equal to 2 or 4 spaces. But phpmyadmin might just decide to not do it that way.
Other example is the line feed (CHAR(10)). In a text editor it would be the signal that the line ends, and (under unix systems mostly) a new line has to start. But you can copy this line feed into a database field, you're just not sure about how it is going to render.
Because they want you to see everything in the cell they may choose to render it as a space... but that's NOT a space if you look at the ascii code of it (and here there's no trick, all rendering engines will tell you the right ascii code).
This is important to always treat characters with their ascii codes.
there's an answer above that suggests using a wildcard instead of the spaces. That might match, or just might not. Let's say your string is '386420K5010', so it is not the one you're looking for, still the LIKE '3864205010%K' pattern would return it. The best is probably to use a regular expression or at least identify the fixed pattern of these strings.
yes as updated question if you wish to remove more space between which contents might be 3 or 4 space below query will use full to you
SELECT REPLACE( REPLACE( part_no, " ", " " ), " ", " " ) from parts.
let me know if it is work for you ?
SELECT *
FROM `parts`
WHERE REPLACE(REPLACE(`part_no`, CHAR(9), ''),' ','') LIKE REPLACE(REPLACE('3864205010 K', CHAR(9), ''),' ','')
This will probably work if part_no and/or search string has tabs and/or spaces.

Mysql, escaped string fields couldn't be searched with "LIKE" keyword

Consider one of the field( sample_field ) in Mysql table has as "your\'s data", when I query the same table as
SELECT * FROM sample_table WHERE sample_field = "your\'s data"
and also like as
SELECT * FROM sample_table WHERE sample_field = "your's data"
both of the above query returns 0 rows, even though the sample_field has the value as "your\'s data"
After a long search I came to know that, my search would be
SELECT * FROM sample_table WHERE sample_field = "your\\\'s data"
that is working very fine. but
SELECT * FROM sample_table WHERE sample_field LIKE "your\\\'s data"
is not working. So if I want to search any product or categories with quotes( like "your's data" ), in site search I must use LIKE keyword with wildcard patterns('%').
Now I have found the answer that if I try like this
SELECT * FROM sample_table WHERE sample_field LIKE "your\\\\'s data"
as given in http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like, it is working fine.
But please let me know why it needs this amount of 4 back slashes to achieve this ?
Thanks in advance
You'll have to escape the escaping backslash, if you want it to be a literal backslash:
WHERE sample_field = "your\\'s data"
But really, having a backslash in your database in the first place is the mistake, fix that instead.
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\n”. To search for “\”, specify it as “\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
Try it like this:
... WHERE sample_field = 'your\'s data'

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