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
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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.