开发者

SQL Syntax Error - PHP/MySQL

Can anyone tell me what's going on here. I'm not sure why this code is not working and throwing the following error:

You have an error in your SQL syntax, check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND role != 7 AND role != 4' at line 3

    $sql = 'SELECT * FROM tblusers
                 INNER JOIN permissions on tblusers.usrID = permissions.user_id
                 WHERE permissions.team_id='.$team_id.' AND role != 7 AND role != 4';

    require("connection.php");

    $result = mysql_db_query($DBname,$sql,$link) or die(mysql_error()); 

    while($row = mysql_fetch_assoc($result))
    {
      $message->addTo($row['usrEmail'],开发者_如何转开发 $row['usrFirst'] . ' ' . $row['usrLast']);
    }

I know that the variable $team_id is working fine, because if I "echo" it, it works fine. Any ideas on what I'm doing wrong here? Thanks!


echo out $sql, try the statement in the database or paste it here so we can debug it. I initially suspected that you needed quotes around the variable but you probably don't since its a number.

Do both tables have a row column or does just one table have it?


I get that exact error message if $team_id is empty - are you sure it's set at that point in the code?


By using prepared statements you can avoid quotes problems.

$dbConn = new mysqli("127.0.0.1", "username", "password", "db");
$stm = $dbConn->prepare("SELECT * FROM tblusers WHERE team_id = ?");
$stm->bind_param("i", $team_id); /* 'i' for an integer */
$stm->execute();


role field is ambiguous try tblusers.role

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜