简体   繁体   中英

Php query returns no rows with varchar

I have a mysql table where the user column is a varchar.

This works with an int type

$result = mysql_query("SELECT name FROM users WHERE id='$id'");

But when I try to match a varchar, nothing gets returned.

$result = mysql_query("SELECT name FROM users WHERE user='$username'");

I've tried to strip the values and still nothing.

any suggestions?

I typically use something like this. (plus some sanitization)

$username = strtolower($username);
$result = mysql_query("SELECT name FROM users WHERE LCASE(user)='{$username}'");

This will get you the username no matter how they type it by making the case identical.

Since you are using = it has to be an exact match. Otherwise you would use LIKE .

The code I provided above will return the exact match of the username. This code will force the $username to all lowercase, then we can do the same for MySQL using LCASE . Now you can have an identical match.

The problem you may have been having was that userName is not the same as UserName or even UsErNaMe . Make sure when you do an exact match that the case is the same too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM