Liam
Liam

Reputation: 9855

Query wont return data once inputs are double digits, PHP/MySQL

I have a query like so...

$levLimit = $_GET['levLimit'];

$sql = "SELECT * FROM users WHERE `level` < '$levLimit'";

This works fine when levLimit is equal to any number up to 9, as soon as I try it with 10 it returns nothing.

This doesn't give me any mysql or php errors neither, is there anything glaringly obvious?

Upvotes: 1

Views: 82

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You need to either cast it (bit of a pain) or change the column type to one that can perform math.

MySQL can't do math on text the way you're trying to query now, only integers.

Caution: Your code is open to SQL injection. Please use a prepared statement for this.

References:

You can also use (int) for the GET array's value:

$levLimit = (int)$_GET['levLimit'];

which won't require a prepared statement but it's always best that you do.


Edit:

"This doesn't give me any mysql or php errors neither."

That is because you enclosed the variable in single quotes in the query which MySQL read that as a string and you say that the column's type is VARCHAR.

  • Had you not used single quotes around the variable, it would have thrown an error.

Upvotes: 6

Related Questions