0

So I am trying to do a fairly simple task, compare a string with a mysql table row/entry using eg.

SELECT * FROM table_name WHERE $string = table_column.

My issue is, is that it is searching and remaining case sensitive.

The string: "A Long String"
The database entry "A Long String"

My table is collated: latin1_swedish_ci which should be searching in case insensitive (like the rest of my queries), I believe since the strings contain whitespace it could be the issue, it is only looking at the first character's capitalisation.

I have tried using 'Upper' and 'lower' comparisons in my queries as well as using 'LIKE' I have also tried removing whitespace using REPLACE but I am probably doing it wrong, not entirely sure. But it never seems to find the correct entry unless I type it as it is in the database.

Any advice would be cool!

$result = mysqli_query($db, "SELECT * FROM character_information WHERE 
(reference LIKE '".$origin_name."' AND char_name LIKE '".$char_name."') 
OR
(lower(reference) LIKE '".$origin_name."' AND lower(char_name) LIKE '".$char_name."') 
OR
(upper(reference) LIKE '".$origin_name."' AND upper(char_name) LIKE '".$char_name."') 
AND 
(char_gender = '".$char_gender."')");

Another try?

$result = mysqli_query($db, "SELECT * FROM character_information WHERE 
reference = '".$origin_name."' AND char_name = '".$char_name."' AND char_gender = '".$char_gender."'");

I know this one is completely wrong, not meant to use REPLACE in this kind of way im assuming, should be used in UPDATE or INSERT? Anyway you can see what I am trying to do by removing the whitespace from everything.

$origin_name = str_replace(' ', '', $origin_name);
$char_name = str_replace(' ', '', $char_name);

$result = mysqli_query($db, "SELECT * FROM character_information WHERE 
( (REPLACE(reference, ' ', '' )) LIKE '".$origin_name."' AND (REPLACE(char_name, ' ', '' )) LIKE '".$char_name."%') 
AND 
(char_gender = '".$char_gender."')");
8
  • Please give the exact code (for example: what is "$string"?). Are you sure that the strings are equal? In some languages characters exist that look similar when displayed but are not (when using Unicode). Commented May 8, 2015 at 12:43
  • I think query must be in this manner SELECT * FROM table_name WHERE table_column = $string . Commented May 8, 2015 at 12:44
  • I used that as an example, I know how to write a mysql query in PHP (i have quotes in it), I already tried using lower and upper. I will post my code. Commented May 8, 2015 at 12:56
  • Okay, yes please post your code. Commented May 8, 2015 at 12:56
  • If you statically pass it a value does it work? Something like SELECT * FROM character_information WHERE char_gender = 'male' Commented May 8, 2015 at 13:00

2 Answers 2

1

Upper and lower comparisons aren't needed at the same time. If you convert both the column in your database and the search term to one or the other as part of the same query, you'll be ensuring case insensitivity.

mysqli_query($db, 'SELECT * FROM table_name WHERE LOWER(table_column) = LOWER(\'' . $value . '\')');

Or for checking if a column contains the value rather than being equal to it:

mysqli_query($db, 'SELECT * FROM table_name WHERE LOWER(table_column) LIKE LOWER(\'%' . $value . '%\')');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip! I will use this if I need it in the future :)
0

I just realised what I was doing wrong,

It WAS returning a result but after I got the result I was comparing both of them in an if statement, eg/ $string == $db_string (which I don't need to, I don't know why I did this) and it was giving me 'false' since PHP takes into account case sensitive data.

Sorry guys!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.