This is how to do it, I have test it and it works:
$sql = " INSERT INTO users (username, password) SELECT * FROM (SELECT '$user', '$phash') AS tmp WHERE NOT EXISTS ( SELECT username FROM users WHERE username = '$user' ) LIMIT 1; ";
This solution is inspired from this answer.
The problem is that you can not combine PHP and MySQL statement like you did, you need to encapsulate all MySQL statements in quote
RAISERROR, it is not MySQL function, it belongs to Microsoft.
You could easily make php if statement that checks if
$sql contain valid username and return your message. That part is left to your fantasy.
XAMPP has no thing to do with the error, it just a software that provides an Apache and MySQL installation for Windows.
Note: P.S. please learn to use parameterized queries, because your
code is vulnerable to SQL injection. thanks to @BillKarwin for mentioning this.
Powered by WPeMatico