Jump to content

Check for User Availability


matvespa

Recommended Posts

I've found this code online, and cant seem to make it work against my database. It kept saying, user is available even though the user exist in the database.

 

UserAvailability.php:

 

<?php

//I've declared the variable according to my database.

 

mysql_connect($host, $db_username, $db_password) or die ("Can't connect to Datebase");

mysql_select_db($db_name) or die ("Couldn't successfully connected");

 

 

$username=$_POST['username'];

$query=("Select * from user where UserEmail='$username'");

$result= mysql_query($query);

$num=mysql_num_rows($result);

if ($num > 0) {//Username already exist

echo "no";

}else{

echo "yes";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/186431-check-for-user-availability/
Share on other sites

I've found this code online, and cant seem to make it work against my database. It kept saying, user is available even though the user exist in the database.

 

UserAvailability.php:

 

<?php

//I've declared the variable according to my database.

 

mysql_connect($host, $db_username, $db_password) or die ("Can't connect to Datebase");

mysql_select_db($db_name) or die ("Couldn't successfully connected");

 

 

$username=$_POST['username'];

$query=("Select * from user where UserEmail='$username'");

$result= mysql_query($query);

$num=mysql_num_rows($result);

if ($num > 0) {//Username already exist

echo "no";

}else{

echo "yes";

}

?>

 

Maybe you should check the session to see if they are online? It should be very simple to enter an entry into their row 'availability' if you don't just want to use sessions to mark availability on a page.

The variable username = useremail

 

Btw how do i check is the session is online?

 

I assume you've a session to turn on as they log in (?)

if (isset($_SESSION['username'])) { 
  //sql statement to tell they're online, aka available
} else { 
  //erase the entry where user is online
}

 

Not sure if this is the best way of coming along it though..

Is your code echoing 'no' or 'yes'?

 

It will echo 'no' if the username (useremail) that was entered was found in your table. It will echo 'yes' if the username was not found and also if your query failed.

 

To help debug if your query worked or failed in the code you are trying, add the following two lines of code immediately after your first opening <?php tag -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

 

Edit: Also, echo $query to see exactly what is in it so that you know your form is submitting the expected data.

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.