Jump to content

Querying database to check username


garry

Recommended Posts

I have implemented an AJAX feature for my registration page (don't move this though please - it's still PHP).

 

It uses jquery and for the plugin i'm using, it has a document called users.php which checks users from a database. Here is the one that came with what I downloaded and works when you input the names into the array:

<?php
$request = trim(strtolower($_REQUEST['username']));
//sleep(2);
usleep(150000);
$users = array('name1', 'name2');
$valid = 'true';
foreach($users as $user) {
if( strtolower($user) == $request )
	$valid = 'false';
}
echo $valid;  ?>

 

Now this works fine, but when I tried to alter this script to query the database to see if username exists, I ran into problems. The problem is that the wrong "true" is returned when something should be false. For some reason it is not getting the correct data. I think it might be something to do with arrays or something but I'm not too sure. Can someone please help?

 

Here's what I was trying, by the way:

 

$mysqli = database::connect();

$users = $mysqli->query('SELECT username FROM users');

$request = trim(strtolower($_REQUEST['username']));
//sleep(2);
usleep(150000);
$valid = 'true';
foreach($users as $user) {
if( strtolower($user) == $request )
	$valid = 'false';
}
echo $valid;

Link to comment
https://forums.phpfreaks.com/topic/111669-querying-database-to-check-username/
Share on other sites

Your best (more efficient) to do this in your actual query.

 

$mysqli = database::connect();

 

if (isset($_REQUEST['username'])) {

  $username = trim(strtolower($_REQUEST['username']));

  if ($users = $mysqli->query("SELECT username FROM users WHERE username = '$username'")) {

    if ($user->num_rows()) {

      echo "Valid";

    }

  }

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.