Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2013
    Location
    Toronto
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Php MySQL: When user not found in database

    I am having trouble writing code that will default to the else provision in a statement when the user is not found in a database query. When I enter a user / pword combination that is found, everything goes as expected.

    Any suggestions on what to do would be appreciated. Thanks

    Here is the code:

    PHP Code:

    $result2 
    mysqli_query($link2"SELECT student_id, stu_name FROM student WHERE student_id = '{$student_id}' AND stu_name = '{$stu_name}'");
        
    // Tests to see if the result fot the query was sucessful.
        
    if($result2 === FALSE){
            die(
    mysqli_error());
        }
        
    // Loops through each item in the array and assigns the contents to the variable name $row2.
        
    while ($row2 mysqli_fetch_array($result2MYSQL_ASSOC)){
            
    // Assigns the number or rows returned to the variable name '$stu_found'
            
    $stu_found mysqli_num_rows($result2) or die(mysqli_error());
            
    // Checks to determine if user input filled.
            
    if (isset($stu_name) && isset($student_id)) {
                
    // Tests to determine if any rows were returned matching the Name and Student ID.
                
    if ($stu_found==1){
                    
    print_r($row2);
                    
    /* This is where the problem appears to exist. When I enter a name and student # combination that is not in the $result2 query it does not default to the else provision*/
                    
    }else{
                    echo 
    "<div style='color:red;font-size:large'>One or more of the fields you entered are invalid.<br>Please enter your correct name and student ID.</div>";
                }
            }
    // Ends if (isset($_POST['stu_name']).
        
    }// Ends while loop. 

  • #2
    New Coder
    Join Date
    Mar 2006
    Location
    Chennai India
    Posts
    34
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by unwiredcoder View Post
    PHP Code:


    if ($stu_found != 1){
                    echo 
    "<div style='color:red;font-size:large'>One or more of the fields you entered are invalid.<br>Please enter your correct name and student ID.</div>";
                } 
    You can have this conditional statement AFTER the WHILE loop construct.
    Chris, Developer, Chrisranjana.com
    Php Developers

  • Users who have thanked chrisranjana for this post:

    unwiredcoder (12-17-2013)

  • #3
    New Coder
    Join Date
    Dec 2013
    Location
    Toronto
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks, It worked.

    I removed the while loop altogether and adjusted an if/else construct I should of used better earlier on in the code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •