简体   繁体   English

MySQL select语句不返回行

[英]MySQL select statement not returning rows

I have a simple SELECT statement that I'm using in PHP: 我有一个简单的SELECT语句,我在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. 我应该返回1行。 I only have one row in the votes table and it contains the username field that matches variable $big_hand_username. 我只在投票表中有一行,它包含与变量$ big_hand_username匹配的用户名字段。 $big_hand_username & $user_name both contain data and I've echoed them out to make sure. $ big_hand_username和$ user_name都包含数据,我已经回应它们以确保。

However, when I just select all from votes table I get 1 row returned, which you'd expect. 但是,当我只从选票表中选择所有时,我会返回1行,这是您所期望的。 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. 这将返回1行,您可以期望它包含一行,其中包含与字段用户名匹配的行。

I just can't understand why I can't get a row returned from votes table using the first select statement. 我只是无法理解为什么我不能使用第一个select语句从投票表中返回一行。 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. 我也尝试在select语句之后运行while循环但是看不到任何错误。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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