简体   繁体   中英

MySQL select statement not returning rows

I have a simple SELECT statement that I'm using in PHP:

$vote=mysql_query("SELECT * FROM `votes` where username = '$big_hand_username' AND 
`voter` = '$user_name'");

When using this statement I don't get any rows returned. I should get 1 row returned. I only have one row in the votes table and it contains the username field that matches variable $big_hand_username. $big_hand_username & $user_name both contain data and I've echoed them out to make sure.

However, when I just select all from votes table I get 1 row returned, which you'd expect. I can also select all from another table using the same variable:

$vote=mysql_query("SELECT * FROM `users` where username = '$big_hand_username'");

This returns 1 row, which you'd expect as it contains a row containing the matching the field username.

I just can't understand why I can't get a row returned from votes table using the first select statement. Am I missing something? Could it be something to do with construction of the table? I've also tried running through while loop after the select statement but can't see anything wrong. Any pointers would be helpful.

Try this

$vote=mysql_query("SELECT * FROM `votes` where username = '".$big_hand_username."' AND 
voter = '".$user_name."'");

Try this

$vote = mysql_query("
            SELECT * FROM votes 
            WHERE username = '{$big_hand_username}' 
            AND voter = '{$user_name}'
        ");

try bellow lines

$sql="
        SELECT * FROM votes 
        WHERE username = '{$big_hand_username}' 
        AND voter = '{$user_name}'
    ";
echo $sql; // for debuging also user_name,big_hand_username check
// for preventing sql-injection
$vote=mysql_query($sql);

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