Viktor Viktor - 8 months ago 25
MySQL Question

Problems with checking for duplicate user in MySQL with php

Im trying to check if the username already exists in the database, but I'm struggling with it. I get an error saying:

Call to a member function bind_param() on boolean in /var/www/public/firsttry/index.php

This is part of my code where I'm trying to implement this:

/*Check for duplicate in DB */
$stmt2 = $mysqli->prepare("SELECT username FROM Users (username, password) WHERE username = ?");

$stmt2->bind_param("s", $username);

$rows= mysqli_num_rows($stmt2);

echo "<script type='text/javascript'>alert('This username is taken!')</script>";

As you can see first I write a prepared statement and try to bind a value into it which is
is assigned to it. I execute the query and then I try to see how many affected rows are returned, if it is more or equal to 1, I throw an alert, else, user is able to register.
Im pretty sure its just some small mistake, but I am kinda new to php so please bear with me. I am aware that my
statement might return false, but why does it complain that I am trying to
on boolean?


Change your select query in:

$stmt2 = $mysqli->prepare("SELECT username FROM Users WHERE username = ?");

And you should use an if statement to have a safe code:

if ($stmt2 = $mysqli->prepare("SELECT username FROM Users WHERE username = ?"))