0

So i'm trying to test if a username being entered is not already stored in the database, as such ->`

  $testU = trim($_POST['Username']);
  $qusertest = "SELECT * FROM user WHERE Username=$testU";
  echo ''.$qusertest.'';
  $rusertest = mysqli_query($dbc, $qusertest);
  echo ''.$rusertest.'';
  $utrow = mysqli_fetch_array($rusertest, MYSQLI_ASSOC);
  if($utrow[1] == $testU){
  $U = trim($_POST['Username']);
} else {
  $errors[] = 'This username has already been taken, please select a new username';
}`

The two echo's are only there to test why the code isn't working. I have a similar setup elsewhere which works, including the $testU variable (except it has a different name, but works in the same way).

When testing the code, i get the following SQL query presented

SELECT * FROM user WHERE Username=test

one account i have in the database has the name "test" and when i search for it using that same SQL in the database i get the result given back. However while doing it through php it gives this error code.

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\ReviewWebsite\Register.php on line 37

Which presumably means that "test" isn't actually working (despite the SQL in MySQL working) and is returning "FALSE" instead of results.

When changing the SQL query to include quotes ( ' ' ) (which i should have to do but i did to test it to see if it would work) i get this error, which leads me to believe that this method is very wrong.

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\wamp\www\ReviewWebsite\Register.php on line 36

Any help would be great, please, because i have no idea what i've overlooked.

Spratters53
  • 395
  • 4
  • 23

0 Answers0