Jump to content

Checking a user exists methods...


PaulRyan

Recommended Posts

This is something that has intrigued me, that has only recently surfaced when viewing the forum.

 

Which of the following methods of authenticating that a user exists would be better/faster/ect?

 

Example 1 - Fetching Row Data

<?PHP
  $username = 'LoserVille';
  $password = 'password';

  $myQuery = mysql_query("SELECT account_id FROM user_accounts WHERE username = '$username' AND password = '$password'");
  $myQuery = mysql_fetch_assoc($myQuery);

  if($myQuery) {
    /*### User Exists ###*/
  } else {
    /*### User Does Not Exist ###*/
  }
?>

 

Example 2 - Fetching Number of Results

<?PHP
  $username = 'LoserVille';
  $password = 'password';

  $myQuery = mysql_query("SELECT account_id FROM user_accounts WHERE username = '$username' AND password = '$password'");
  $myQuery = mysql_num_rows($myQuery);

  if($myQuery >= 1) {
    /*### User Exists ###*/
  } else {
    /*### User Does Not Exist ###*/
  }
?>

 

Just looking for some insight, not really a problem :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

I wouldn't use or die() for anything. There's numerous posts around saying why.

 

The method I suggested is (IMO) the correct way.

 

<?php

$username = 'LoserVille';
$password = 'password';

$myQuery = "SELECT account_id FROM user_accounts WHERE username = '$username' AND password = '$password'";
  
if ($result = mysql_query($myQuery)) {
  if (mysql_num_rows($result)) {
    // user exists
  } else {
    // no user found
  }
} else {
  // query failed.
  trigger_error(mysql_error() . "<br />$myQuery");
}
?>

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.