Hello! So I am trying to get a feature of my code working where I check if the username/email is already taken. I am hitting a roadblock though with a weird error about syntax. But another query with almost the same syntax works fine! Here is the error:
) Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(username, email) VALUES('test','123@45.com')' at line 1 in C:\xampp\htdocs\Refactored\lib\DatabaseHelper.class.php on line 27
and
( ! ) PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(username, email) VALUES('test','123@45.com')' at line 1 in C:\xampp\htdocs\Refactored\lib\DatabaseHelper.class.php on line 27
This is my code for that function, everything else is working fine:
public function createUser($user) {
$sql = "SELECT * FROM users(username, email) VALUES(?,?)";
$statement = DatabaseHelper::runQuery($this->connection, $sql, Array($user->getUsername(), $user->getEmail()));
$row = $statement->fetch();
if($row->rowCount() > 0) {
if ($username == $row['username']) {
echo "Username exists";
} elseif ($email == $row['email']) {
echo "Email exists";
}
} else {
$hashed_password = password_hash($user->getPassword(), PASSWORD_DEFAULT);
$sql = "INSERT INTO users(username, password, email) VALUES(?,?,?)";
$statement = DatabaseHelper::runQuery($this->connection, $sql, Array($user->getUsername(), $hashed_password, $user->getEmail()));
}
}
I think this is the problem:
$sql = "SELECT * FROM users(username, email) VALUES(?,?)";
But it looks correct and I feel the rest of my code is correct. I do not know what MariaDB is but maybe my database is hitting a problem?
Thanks in advance for any help!