1

I am trying to create a duplicate username checker and I am thinking this is probably the way to do it correct me if im wrong. Basically I want the username the user input to be stored in a variable called userName and then use that variable to check and see if there are any LIKE rows in the database and if so return a count of 1 or more to a variable named $count I would then have an IF ELSE statement that would either yell at the user or let them continue. I have run into a problem using the LIKE statement. I think my syntax could be wrong since I have been trying several different methods but still no luck.

Main Code

<?php

require 'DB.php';
$userName = "tes";
echo $userName;
try{
$stmt = $conn->prepare('SELECT COUNT(*) FROM `CLL_users` WHERE `user_name` LIKE "% . ":userName" . "');
$stmt->bindValue(':userName', $userName);
$stmt->execute();
$count = $stmt->fetchColumn();
return $count;
echo $count;

} catch (PDOException $e){
    echo 'Connection failed: ' . $e->getMessage();

    }


?>
4

3 回答 3

2

It appears you're trying to do string concatenation in MySQL using invalid syntax. Try this:

'SELECT COUNT(*) FROM `CLL_users` WHERE `user_name` LIKE CONCAT("%",:userName)'
于 2012-11-04T20:09:00.797 回答
0

You can probably use = instead of LIKE. The SQL syntax in itself isn't case-sensitive. I think you might be able to set up your database so that certain tables are case sensitive but I know for a fact through usage that no tables in my own MySQL database are. If you're unsure it's easy to try, just run a select with a username you know is in there but write it with different casing.

Just do a select WHERE 'user_name'=$entered_username, if you get one or more don't add the new user, if not go ahead. You could of course mark the user_field as unique, that way you'd know for sure you won't have duplicates, might be something worth looking at.

于 2012-11-04T20:18:28.730 回答
-2

Try to use this select:

("SELECT COUNT(*) FROM CLL_users WHERE user_name LIKE "% . "'".$userName."'");

于 2012-11-04T19:54:10.343 回答