Rajan Benipuri
Rajan Benipuri

Reputation: 1832

Why comparing Strings in MySQL query is not working

I am running a very simple SELECT query in MySQL and it's not working.

SELECT string_name FROM table_name;

This is giving me required output. Like

  1. This is string one.
  2. This is string two.
  3. This is string three.

and so on...

But if I am running a query like this

SELECT * FROM table_name WHERE string_name='This is string one'

It's not giving any output. I even tried TRIM function.

SELECT * FROM table_name WHERE TRIM(string_name)=TRIM('This is string one')

But it's still not giving any output.

Please suggest what I am missing here. Is it because of some formatting or am I doing any silly mistake. By the way, Strings are saved as VARCHAR in the database.

Upvotes: 0

Views: 5542

Answers (3)

Rajan Benipuri
Rajan Benipuri

Reputation: 1832

First of all, I must acknowledge the points made by @Uueerdo were actually the the main cause of this issue. Even I was somewhat sure that there are some hidden characters in the string causing all the issue but I was not sure how to find and fix that offending character.

Also, the approach suggested by @Uueerdo to check and replace the offending character using the ASCII code seems quite legit but as he himself mentioned that this process will take lot's of time and one have to manually check every string for that one offending character and then replace it.

Luckily after spending couple of hours on it, I came up with a much faster approach to fix the issue. For that, first of all I would like to share my use case.

My first query was for selecting all the strings from a database and printing the result on page.

$result = mysqli_query($conn, "SELECT * from table_name");
while($row = mysqli_fetch_array($result)){
   $string_var = $row["string_name"]; 
   echo $string_var;
   echo "<br>";
}

The above code was working as expected and printing all the string_name from the table. Now, if I wanted to use the variable $string_var for another SELECT query in the same table, it was giving me 0 results.

$result = mysqli_query($conn, "SELECT * FROM table_name");
while($row = mysqli_fetch_array($result)){
    $string_var = $row["string_name"];
    echo "String Name : ".$string_var."";
    $sec_result = ($conn, "SELECT * FROM table_name WHERE string_var='$string_name'");
    if(mysqli_num_rows($sec_result) > 0){
        echo "Has Results";
    } else {
        echo "No Results";
    }
}

In this snippet, my second query $sec_result was always giving me No Results as output.

What I simply did to fix this issue.

$result = mysqli_query($conn, "SELECT * FROM table_name";
while ($row = mysqli_fetch_array($result)){
    $string_var = $row["string_name"];
    $row_id = $row["id"];

    $update_row = mysqli_query($conn, "UPDATE table_name SET string_name='$string_var' WHERE id=$row_id");
}

This step updated all the strings from the table without any hidden/problem causing character.

I am not generalising this approach and I am not sure if this will work in every use case but it helped me fix my issue in less than a minute.

I request @Uueerdo and others with better understanding on this to post a more generic approach so that it can help others because I think many people who can't find a right approach in such conditions, end up using LIKE in place of = but that completely changes the core idea of the query.

Upvotes: 0

Uueerdo
Uueerdo

Reputation: 15941

To reiterate from comments; sometimes "non-printing" control characters (like newlines) can make their way into data they were never intended to be a part of. You can test for this by checking CHAR_LENGTH of field values versus what you actually see. Obviously, on large amounts of data this can be difficult; but if you know of one problematic value already, you can use this method to confirm this is the problem on that row before attempting to identify the offending character.

Once this problem is confirmed, you can use queries with MySql's ASC() and substring functions to identify character codes until you find the character; it can be best to start from the end of the string and work back, as often the offending characters are at the end.

The character or characters identified in known problem rows are often the cause of other problem rows as well, so identifying the issue in one known row can actually help resolve all such problems.

Once the character code(s) are identified, queries like WHERE string_name LIKE CONCAT('%', CHAR(13), CHAR(10)) should work (in this case for traditional Windows newlines) to identify other similar problem rows. Obviously, adjust character codes and wildcards according to your circumstances.

If no row should ever have those characters anywhere, you should be able to clean up the data with an update like this: UPDATE theTable SET theString = REPLACE(REPLACE(theString, CHAR(10), ''), CHAR(13), '') to remove the offending characters. Again, use the codes you've actually observed causing the problem; and you can convert them to spaces instead if circumstances are better handled that way, such as a newline between two words.

Upvotes: 3

marcoverbeek
marcoverbeek

Reputation: 74

Have you tried using LIKE for debugging purposes?
SELECT * FROM table_name WHERE string_name LIKE 'This is string one'

/!\ Don't just switch from = to LIKE, read about why here
TLDR:

  1. = is apparently 30x faster.
  2. Use = wherever you can and LIKE wherever you must.

Upvotes: 0

Related Questions