Find Numeric code string in a SELECT mysql statement with LIKE - php

I save the single quote in my utf8 mysql database with ' .
now I just want to select all rows that include a ' in the name column. So I do:
SELECT * WHERE name LIKE "%'%"
but this will give me an empty result. if I do
SELECT * WHERE name LIKE "%&#%"
for example its working great.
What I am doing wrong? I want to search for the complete string '.

SELECT * FROM testone WHERE val LIKE '&#39'
result
From Table

Here is a working example of your query:
https://www.db-fiddle.com/f/ci63XiAPbKLaz7bRksJ4gW/1
The problem may depend on something else, e.g. how the ' is stored in the database.
May be the case that the ' is inserted as is, so try this:
SELECT * FROM test WHERE name LIKE "%'%";
In the same db-fiddle you can try this.
You can perform the search for both cases in the same query:
SELECT * FROM test WHERE (name LIKE "%'%" OR name LIKE "%'%");

Related

SQL WHERE CONCAT LIKE NOT

I try to make SQL to search some string in database.
In this spesification, The SQL must be dont display one string in database.
my sql like this :
$query = "SELECT * FROM `chatuser` WHERE CONCAT( `fullname`,`image`) LIKE '%".$search_string."%' NOT (`$string is not be displayed`) " ;
is that possible ?
Thanks for help
The correct syntax of LIKE and NOT LIKE as two conditions would be:
SELECT * FROM chatuser
WHERE CONCAT(CustomerName,ContactName) LIKE '%t%'
AND CONCAT(CustomerName,ContactName) NOT LIKE '%m%';
You miss AND Between conditions. Also you have to repeat CONCAT(CustomerName,ContactName).
In the example above we are looking for all CustomerName+ContactName with a t in any place but if it doesn't have an m in any place.
From the docs found at https://www.w3resource.com/mysql/comparision-functions-and-operators/not-like.php
Example: MySQL NOT LIKE operator with (%) percent
The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.
Code:
SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE 'W%';
And so it seems would work in your situation.

How to fetch value from table as per multiple id using MySQL

I need one help. I need to fetch value from table as per multiple ids which are in comma separated string using MySQL. I am explaining my table and query below.
db_basic:
id special name
1 2,3,4,5 Raj
2 4,2,5,6 Rahul
3 3,5,6 Rocky
My code is given below.
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where special='".$special."'");
Here I need where special=2 is present inside that comma separated string those value will be fetched. I need only proper query. Please help me.
use below query
May be work for u
Use LIKE instead of "="
"select * from db_basic where special LIKE '%".$special."%'"
use FIND_IN_SET function for ex.
select * from db_basic where FIND_IN_SET(2,special)
Try this once,
select * from db_basic where FIND_IN_SET($special, special);
It should work.
if you current code expeted to return the first and second line of your db, you can use the LIKE operator in the query like this :
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where special LIKE '%,".$special.",%'");
or LOCATE like :
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where LOCATE(".$special.",special)>0");
And FIND_IN_SET : From the docco here - "This function does not work properly if the first argument contains a comma (“,”) character".

MySQL Query- Issue while using like operator

I have a table names and in that table there is a field info data is like this:
ID info
1 'alpha,romeo,ciera,delta'
2 'testing,temp,total'
I am trying to create a select query.
select * from names where info like '%$var%'
$var is data from php.
Problem is i want exact match. If i use above query and in $var if
data is rome then it also return row of romeo.
one more example-
data in table is testing,temp,total
user input data is test then it also return testing
i tried
select * from names where info like '$var%'
and
select * from names where info like '%$var'
but it didn't return data as i expected.
Please advise how can i achieve this.
Note- : This is an example i am not using mysql as its depreciated. I am using mysqli
Append , at begin and end of your target search string.
And then make sure your source string also has those ,
SQL Fiddle Demo
select *
from names
where concat( ',', info , ',') like
concat( '%,', $var, ',%')
The problem is this wont use any index. You should go for FULL TEXT search
Problem is i want exact match. If i use above query and in $var if data is rome then it also return row of romeo.
Don't use the LIKE operator, use exact match operator =
select * from names where info = '$var'
Use , in your query to use it as a delimiter and use multiple conditions to account for the "edge cases".
SELECT *
FROM names
WHERE
info LIKE '%,$var,%' OR
info LIKE '$var,%' OR
info LIKE '%,$var' OR
info = '$var'
If you have rows with info column:
alpha,romeo,ciera,delta
testing,temp,total
rome
foo,test,bar
berlin,paris,madrid,london,rome
venice,milano,rome,firence
black,crome
rome,fome,mome,kome,kome
Query with $var as "rome" will select:
rome
berlin,paris,madrid,london,rome
venice,milano,rome,firence
rome,fome,mome,kome,kome
But not:
alpha,romeo,ciera,delta
black,crome

How to search for a comma-separated string in a database table in mySQL

How to search for a comma-separated string in a database table in mySQL.
Suppose, I have a string in variable $facilities='breakfast,dinner,lunch' and in my database I have a string saved in a field called facilities having values breakfast,dinner,clothing,lunch,hot water.
How do I get the row having values $facilities?
Work like this i hope this is your requirement
select * from table where field_name like '%$fieldname%'
You're going to need to break apart the comma separated string first, and include a clause for each, so for that input you'd generate SQL like (assuming you want rows that have all three, and maybe other flags set):
SELECT *
FROM YourTable
WHERE FIND_IN_SET('breakfast', facilities)
AND FIND_IN_SET('dinner', facilities)
AND FIND_IN_SET('lunch', facilities)
You can break the string apart in PHP with something a little like:
$facilities='breakfast,dinner,lunch';
$desired = explode ( "," , $facilities );
And just use a loop to build your SQL
SELECT *
FROM tableName
WHERE FIND_IN_SET('lunch', 'breakfast,dinner,lunch')
EDIT:
SELECT *
FROM tableName
WHERE FIND_IN_SET('lunch', food_column)
food_column is the name of the column where the string 'breakfast,dinner,lunch' is stored

How to search mulitple value seperated by commas in mysql

How to search multiple values separated by commas.
ex:
table name : searchTest
id name keyword
1 trophy1 test1,test2,test3
2 trophy2 test2,test5
Points:
If i search for test2 both results trophy1 and trophy2 should be display.
If i search for trophy1 then trophy1 should be as result.
How to solve this issue.
thanks in advance
I would say that, here, your data structure is quite not right.
It would be a better solution to not store several values in one field using some comma-separated format, but use three tables, defined this way :
searchtest
id
name
keywords
id
word
keywords_searchtest
id_keyword
id_searchtest
With that, searching for entries in searchtest that have specific keywords would be as simple as :
select searchtest.*, keywords.*
from searchtest
inner join keywords_searchtest on keywords_searchtest.id_searchtest = searchtest.id
inner join keywords on keywords.id = keywords_searchtest.id_keyword
where keywords.word = 'test2'
And, additionnaly, you'd have to search for searchtest entries with a specific name :
select *
from searchtest
where name = 'trophy1'
These keywords must be stored in the separate table
For Point 1 :
select * from searchTest where keyword like LIKE '%test2%
For Point 2 :
select * from searchTest where name like LIKE 'trophy1%
Use the FIND_IN_SET MySQL string function, like so:
SELECT * FROM searchTest WHERE FIND_IN_SET('test2', keyword) > 0
You can use like.
select * from searchTest where keyword like '%test2%'
Where the % is a wildcard.
select * from searchTest where keyword LIKE 'test2' or LIKE '%,test2' or LIKE 'test2,%'
it will work i have done this using above query

Categories