I\'ve looked at the many other posts that were similar to my issue and implemented their solutions (as far as I can tell) as exactly as I could. However, every time I execut
Here's one that i wrote:
$error = false;
$sql= "SELECT username FROM users WHERE username = '$username'";
$checkSQL = mysqli_query($db, $checkSQL);
if(mysqli_num_rows($checkSQL) != 0) {
$error = true;
echo '<span class="error">Username taken.</span>';
}
Works like a charm!
change your query to like.
$username = mysql_real_escape_string($username); // escape string before passing it to query.
$query = mysql_query("SELECT username FROM Users WHERE username='".$username."'");
However, MySQL is deprecated. You should instead use MySQLi or PDO
TRY THIS ONE
mysql_connect('localhost','dbuser','dbpass');
$query = "SELECT username FROM Users WHERE username='".$username."'";
mysql_select_db('dbname');
$result=mysql_query($query);
if (mysql_num_rows($query) != 0)
{
echo "Username already exists";
}
else
{
...
}
Try this:
$query = mysql_query("SELECT username FROM Users WHERE username='$username' ")
Don't add $con
to mysql_query()
function.
Disclaimer: using the username
variable in the string passed to mysql_query
, as shown above, is a trivial SQL injection attack vector in so far the username
depends on parameters of the Web request (query string, headers, request body, etc), or otherwise parameters a malicious entity may control.
PHP 7 improved query.........
$sql = mysqli_query($conn, "SELECT * from users WHERE user_uid = '$uid'");
if (mysqli_num_rows($sql) > 0) {
echo 'Username taken.';
}
Everything is fine, just one mistake is there. Change this:
$query = mysql_query("SELECT username FROM Users WHERE username=$username", $con);
$query = mysql_query("SELECT Count(*) FROM Users WHERE username=$username, $con");
if (mysql_num_rows($query) != 0)
{
echo "Username already exists";
}
else
{
...
}
SELECT *
will not work, use with SELECT COUNT(*)
.