Hello,

I have a table with a filed Customers defined as int(10)
I have the following query:
SELECT * FROM 'table' where Customers = 'a'
This query returns all entries which have 0 in this field, BUT a is not equal to 0 as we know?

Also I can use any string, not only 'a'.

BUT on other database the same query works as expected. It doesn't return any entries.

Any explanation ?

Most likely the 'a' is implicitly converted to an int, resulting in 0 as default value on failure.

Member Avatar for diafol

Why are you searching a field you know to be of type INT with a string?

You should validate all data prior to using the query. Even your query string has the single quotes, suggesting a string. Doesn't make sense.

You have posted in PHP, so I assume that you're using mysqli or PDO (if not, you should be). You'd most likely be using a prepared statement, binding the typed value (INT).

AFAIK, if MySQL can't parse strings destined for INT fields (i.e. not a string integer, like '9'), it will treat it as 0 (zero).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.