0

I am trying to search the database to see if the username is already taken. I know there are a lot of stuff on 'how to check' but I would like help on why it is giving me the die('Error getting information'); message. I'm not sure if its my query or what.

This is my PHP

$conn = new mysqli($servername, $username, $password, $dbname);

$username = $_POST["username"];

$sqlget = "SELECT username FROM users WHERE $username = username";
$sqldata = mysqli_query($conn, $sqlget) or die('Error getting information');
$sql_row_count = mysqli_num_rows($sqldata);


if ($sql_row_count > 0) {
  echo 'Records found!';
} else {
  echo 'No records found!';
}

So when I enter a username and click submit it will show me "Error getting information" Could you guys tell me why?

Ash
  • 11
  • 6
  • **exectly** because you're using die, instead of proper error reporting – Your Common Sense Mar 01 '17 at 15:43
  • 1
    @YCS, I can half understand why this has been closed but the question asked wasn't actually how to get the error, it was whats causing the error.. – Option Mar 01 '17 at 15:52
  • Yeah I don't understand why its been duped as that question either. Im not asking how do I get the error im asking why – Ash Mar 01 '17 at 15:55

1 Answers1

0

$sqlget = "SELECT username FROM users WHERE $username = username";

Should be:

$sqlget = "SELECT username FROM users WHERE username ='$username'";

Though you should bind the $username as you're vulnerable to SQL Injections.

Option
  • 2,605
  • 2
  • 19
  • 29