phpmysqlpdobindvalue

bindParam & bindValue don't work?


I'm trying to make a register/login system. To check if usernames and email addresses aren't used, I use this :

$username = $_POST['uLogin'];
    $usernameLC = strtolower($username);
    $query1 = $db0->query("SELECT userLogin FROM tbuser WHERE userLogin=':login';");
    $query1->bindValue(":login", $usernameLC, PDO::PARAM_STR);

But it doesn't work. I can create as much users with the same username as I want. By extension, it also won't let me connect onto the website as it doesn't bind values or anything, so it can't compare my username to the one in the DB.

Verifying if a username is not taken worked when I used it like this

$username = $_POST['uLogin'];
    $usernameLC = strtolower($username);
    $query1 = $db0->query("SELECT userLogin FROM tbuser WHERE userLogin='$usernameLC';");

But it isn't the proper way to go :/

Can anybody help ? :)


Solution

  • First off, if you're going to prepare, use ->prepare(), and remove quotes in your named placeholders, they don't need to have that:

    $query1 = $db0->prepare("SELECT userLogin FROM tbuser WHERE userLogin= :login");
    

    Then $query1->execute(), the prepared statement after the binding, so all in all:

    $username = $_POST['uLogin'];
    $usernameLC = strtolower($username);
    $query1 = $db0->prepare('SELECT userLogin FROM tbuser WHERE userLogin = :login'); // prepare
    $query1->bindValue(':login', $usernameLC, PDO::PARAM_STR); // bind
    $query1->execute(); // execute