i need to query my database and find results :
mysql_query("select * from ".ALU_TABLE." where username like '%$q%' or name like '%$q%'");
if i have a name in my table such as Book and i enter book in search box it wont show the Book
i need to query my database as not to be case sensitive.
You can use the LOWER() function
... WHERE LOWER(username) LIKE blabl OR LOWER(name) LIKE asdasd
You need to append something like COLLATE utf8_general_ci to your query. (The _ci suffix stands for case insensitive.)
Have a look at the documentation: 9.1.7.1. Using COLLATE in SQL Statements:
With the COLLATE clause, you can override whatever the default collation is for a comparison. COLLATE may be used in various parts of SQL statements.
Related
I am trying to make an accent insensitive search using collate. But it doesn't work. Furthermore, I heard that my code below might not be optimal if my database becomes really large. Thus, any ideas on how to make this code efficient even for large databases is highly appreciated as well.
<?php
$search_FirstName="%".$_POST['First_Name']."%";
$search_LastName="%".$_POST['Last_Name']."%";
$search_Email="%".$_POST['Email']."%";
$stmt = $con->prepare('SELECT * FROM persons WHERE FirstName LIKE ? collate utf8_general_ci AND LastName LIKE ? collate utf8_general_ci AND Email LIKE ? collate utf8_general_ci');
$stmt->execute([$search_FirstName,$search_LastName,$search_Email]);
?>
I think that you can directly change the collation of your database to latin1_swedish_ci.
And after you could directly write your request like that :
$stmt = $con->prepare('SELECT * FROM persons WHERE FirstName LIKE ? AND LastName LIKE ? AND Email LIKE ?');
Is there any way I can write a select query with where clause, that ignores non-latin characters?
For example there is Lithuanian character Ė, obviously the equivalent in latin character would be E. So is there anyway I can write a query like this:
SELECT * FROM `table` WHERE `keyword` LIKE %E%;
And I want it to return all records that contain any of these characters: E,Ė,Ę, is there any way I can achieve this automatically with SQL (or even in PHP level)?
You don't say what you've tried. You also don't say what character set (utf-8) and collation your table contains. Those are the ways you control those things by default.
You could try this:
SELECT * FROM `table` WHERE `keyword` COLLATE utf8_general_ci LIKE %E%
Or this
SELECT * FROM `table` WHERE `keyword` COLLATE utf8_lithuanian_ci LIKE %E%
I don't know if the second one will work for what you want, because I don't know Lithuanian. Are E, Ė, and Ę considered the same letter in the dictionary? If they are not, then the Lithuanian collation won't match them to each other.
If you do find that a particular collation works, you can alter your column to use that collation by default.
alter table `table`
change `keyword` `keyword` collate utf8_lithuanian_ci
That's a good idea because then you have a chance of indexes speeding your search.
I have a problem with PHP and MySQL:
An user with account:
username: StAr
password: 4LasK4
Can login with:
username: star
password: 4lask4
Uppercases are not respected.
What may be the problem?
Thanks
The collation determines whether two strings are equal. You should change the collation for the username and password columns to case sensitive or binary. For example:
alter table TableName modify username varchar(xx) collate utf8_bin,
modify password varchar(xx) collate utf8_bin;
Check out the column definition and make sure your collation is not case insensitive (ci). You can try with this:
alter table your_table modify username varchar(xx) collate utf8_bin
Many MySQL installations have a default collation setting of utf8_general_ci. This makes these two operations yield precisely the same result.
SELECT * from users WHERE name = 'StAr'
SELECT * from users WHERE name = 'star'
You can try ALTERing the collation for your name column in your database. Or you can use a query like this:
SELECT * from users WHERE name = 'StAr' COLLATE utf8_bin
This utf8_bin collation compares for precise equality. If you put the COLLATE in your query, however, you may disable the use of an index for the query and cause MySQL performance trouble.
I want to filter my columns with a like in select clause, the default like in MySQL is case-insensitive, but I want it to be case-sensitive. How to do it?
I want this query:
select * from my_table where column1 like '%abc%'
and this query:
select * from my_table where column1 like '%Abc%'
to give different results.
The default character set and collation are latin1 and latin1_swedish_ci, so nonbinary string comparisons are case insensitive by default. This means that if you search with col_name LIKE 'a%', you get all column values that start with A or a. To make this search case sensitive, make sure that one of the operands has a case sensitive or binary collation. For example, if you are comparing a column and a string that both have the latin1 character set, you can use the COLLATE operator to cause either operand to have the latin1_general_cs or latin1_bin collation:
col_name COLLATE latin1_general_cs LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_general_cs
col_name COLLATE latin1_bin LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_bin
MySQL :: MySQL 5.6 Reference Manual :: C.5.5.1 Case Sensitivity in String Searches (emphasis added)
case-insensitive search of MySQL?
For my site search, what is the most efficient way of to query my db for a word/phrase regardless of case?
If your database / table is not set up with a case-insensitive collate you need to append something like COLLATE utf8_general_ci to your query. (The _ci suffix stands for case insensitive.)
Have a look at the documentation: 9.1.7.1. Using COLLATE in SQL Statements:
With the COLLATE clause, you can override whatever the default collation is for a comparison. COLLATE may be used in various parts of SQL statements.