MySQL错误#1064无法找到解决方案?

时间:2015-12-09 19:04:36

标签: php mysql

我意识到错误与语法有关 - 我对MySQL很新;但我通常知道我的方式。这个错误一直困扰着我好几天!任何和所有的帮助将不胜感激,谢谢!

ERROR:

  

已成功连接! SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,以获得在''附近使用的正确语法。在第1行USER EXISTS

CODE(我把它缩小到这两个函数,addUser()调用doUserExist()):

function doesUserExist($user, $conn) {

    try {

        // Setting the query and runnin it...  
        $sql = "SELECT COUNT(email) FROM userinformation WHERE email = $user";
        $result = $conn->query($sql);

        // If it's greater than 0 then the account exists
        if ($result != 0) {
            echo "more than 0";
            return true; // user exists
        } else {
            echo "0";
            return false; // user does not exist
        }
    }
    // Catching it if something went wrong.
    catch(PDOException $e) {
        echo $e->getMessage();
    }
} 

function addUser($user, $conn) {
    echo $user;
    // If user does not exist

    if (doesUserExist($user, $conn) === false) {
        // Add user to database (begin this process)
        $sqlEntry = "INSERT INTO userinformation (email, password) VALUES('alexnord', 'password')";
        $query = $conn->query($sqlEntry); // insert data into table

     } else {
         // Take to profile homepage
         echo "USER EXISTS";
     }
}

1 个答案:

答案 0 :(得分:0)

你忘记了字符串输入的单引号,你的选择查询应该是

  $sql = "SELECT COUNT(email) FROM userinformation WHERE email = '$user'";