Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL returns all rows when field=0

I was making some tests, and it was a surprise when i was querying a table, and the query SELECT * FROM table WHERE email=0 returned all rows from the table.

This table has no '0' values and it's populated with regular e-mails.

Why this happens? This can lead to serious security problems.

Is there a way to avoid this without modifying the query?

Am i missing something here?

Thanks.

like image 242
AFRC Avatar asked Oct 24 '11 19:10

AFRC


People also ask

How to SELECT records with NULL values in MySQL?

To look for NULL values, you must use the IS NULL test. The following statements show how to find the NULL phone number and the empty phone number: mysql> SELECT * FROM my_table WHERE phone IS NULL; mysql> SELECT * FROM my_table WHERE phone = ''; See Section 3.3.

How to use NULL in MySQL?

Let's look at an example of how to use MySQL IS NULL in a SELECT statement: SELECT * FROM contacts WHERE last_name IS NULL; This MySQL IS NULL example will return all records from the contacts table where the last_name contains a NULL value.

How to set NULL value in MySQL workbench?

You can right-click on a cell and choose "Clear Field Content" in the popup menu. It will set the cell value to NULL.

How to insert empty value in MySQL?

You use NULL : insert into table tablename values (12, 13, 19, NULL, NULL, NULL, NULL, 1, NULL, 2, NULL, 5); You can also use '' if the column is a string and by "blank" you mean "empty string".


1 Answers

This is because it is converting the email field (which I assume is a varchar field) to an integer. Any field without a valid integer will equate to 0. You should make sure that you only compare string fields to string values (same goes for dates, comparing to dates). The query should be as follows.

SELECT * FROM table WHERE email='0';
like image 167
Kibbee Avatar answered Sep 18 '22 13:09

Kibbee