Jump to content

Recommended Posts

$queryUser = "SELECT username FROM user WHERE username = '$username' AND password = '$password'";

$resultUser = mysql_query($queryUser) or die (mysql_error());

$rowUser = mysql_fetch_assoc($resultUser);

 

if the password and username don't match any records what will $rowUser['username'] be equal to?

 

0 or null or something else?

Link to comment
https://forums.phpfreaks.com/topic/141840-question-about-query-hitting/
Share on other sites

From the php manual for mysql_fetch_assoc -

Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

 

$rowUser will be a bool FALSE value when there is no matching row. $rowUser['username'] won't exist.

 

You can either use mysql_num_rows() to test how many rows are in the result set, like Maq posted, or you could put the mysql_fetch_assoc() in a conditional statement -

 

if($rowUser = mysql_fetch_assoc($resultUser)){
// there was a row in the result set
} else {
// there was not a row in the result set
}

I want to check if the login is valid, and I also want to check if the user is verified....column verified set to 1.

<?php
$queryUser = "SELECT userID,username,fname,lname,verified FROM user WHERE username = '$username' AND password = '$password'";
$resultUser = mysql_query($queryUser) or die (mysql_error());
$rowUser = mysql_fetch_assoc($resultUser);
$row_count = mysql_num_rows($resultUser);
if ($row_count==1 && $rowUser['verified']==0)	//***********	LOGIN FAILED, AWAITING VERIFICATION	
{
$loginFailureMessage = "Your account is awaiting verification";
}elseif ($row_count==1 && $rowUser['verified']==1)//**********	LOGIN SUCCESS, SET VARIABLES, SET SESSION VARIABLES
{
//do all that stuff

}elseif ($row_count==0)		//***********	LOGIN INVALID
{
$loginFailureMessage = "Your username and password did not match any records";
}		

 

does that seem correct?

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.