0

I realize the error pertains to syntax - I am fairly new to MySQL; but I usually know my way around. This error has been plaguing me for days! Any and all help would be appreciated, thanks!

ERROR:

Connected successfully! SQLSTATE[42000]: Syntax error or access violation: 1064 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 '' at line 1 USER EXISTS

CODE (I narrowed it down to these two functions, addUser() calls doesUserExist()):

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";
     }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Alex
  • 21
  • 3
  • Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). – Jay Blanchard Dec 09 '15 at 19:07

1 Answers1

0

you forget single quotations for string input, your select query should be

  $sql = "SELECT COUNT(email) FROM userinformation WHERE email = '$user'";
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38