limitphp Posted January 21, 2009 Share Posted January 21, 2009 $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 More sharing options...
Maq Posted January 21, 2009 Share Posted January 21, 2009 I usually do: if (mysql_num_rows($resultUser) > 0) { echo "match"; } else { echo "invalid"; } But in your case I think you can just do: if($rowUser['username']) { echo "match"; } else { echo "invalid"; } Link to comment https://forums.phpfreaks.com/topic/141840-question-about-query-hitting/#findComment-742628 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2009 Share Posted January 21, 2009 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 } Link to comment https://forums.phpfreaks.com/topic/141840-question-about-query-hitting/#findComment-742645 Share on other sites More sharing options...
limitphp Posted January 21, 2009 Author Share Posted January 21, 2009 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? Link to comment https://forums.phpfreaks.com/topic/141840-question-about-query-hitting/#findComment-742648 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.