无法从MySQL表中提取动态信息

时间:2014-03-31 13:13:42

标签: php mysql

我有2个数据库,我正在连接并尝试运行查询,如果我从一个获得结果,我无法将它们与另一个匹配,但是,如果我回显结果并将其输入查询手动,它工作正常。有什么想法吗?

//USER SETTINGS STUFF
$users = "SELECT * FROM user_info
        WHERE frequency='daily'";
$result = mysqli_query($sql, $users);
while($row = mysqli_fetch_array($result)) {
    //EMAIL LOGIN STUFF 
    $username = $row['user_name'] . '<br />';
    echo $username . '<br />'; //echos "dustin"
    $emailquery = "SELECT * FROM users
                WHERE user_name = 'dustin'";
    $eresult = mysqli_query($loginsql, $emailquery);

    while($row = mysqli_fetch_array($eresult)) {
        $emailaddress = $row['user_email']; 
        echo $emailaddress; 
        }//END EMAIL LOGIN WHILE
}//END USER SETTIGS WHILE

上述代码中的此查询完美无缺,并返回相应的电子邮件地址

$emailquery = "SELECT * FROM users
WHERE user_name = 'dustin'";

下面的查询(这就是我需要它)不会返回任何内容

$emailquery = "SELECT * FROM users
WHERE user_name = '$username'";

关于为什么的任何想法?

2 个答案:

答案 0 :(得分:0)

这是因为你在$username var的末尾添加换行符。只需删除<br />并检查发生了什么。

替换此行:

$username = $row['user_name'] . '<br />';

有了这个:

$username = $row['user_name'];

答案 1 :(得分:-1)

试试这个

$emailquery = "SELECT * FROM users WHERE user_name = '" . $username ."'";