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
https://forums.phpfreaks.com/topic/222164-checking-a-user-exists-methods/
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");
}
?>

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.