SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,934
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question about WHILE loop

    I have a question about error-handling and my WHILE loop.

    First, here is the code for my query...
    PHP Code:
            $q8 "SELECT COALESCE(m.username,'Non-Member') AS username
                    FROM visitor_log AS v
                    LEFT OUTER JOIN member AS m
                    ON m.id = v.visitor_id
                    WHERE v.member_viewed_id=?
                    ORDER BY v.created_on DESC
                    LIMIT 10"
    ;

            
    // Prepare statement.
            
    $stmt8 mysqli_prepare($dbc$q8);

            
    // Bind variable to query.
            
    mysqli_stmt_bind_param($stmt8'i'$memberID);

            
    // Execute query.
            
    mysqli_stmt_execute($stmt8);

            
    // Store results.
            
    mysqli_stmt_store_result($stmt8);

            
    // Check # of Records Returned.
            
    if (mysqli_stmt_num_rows($stmt8)>0){
                
    // Visitors Found.

                // Bind result-set to variables.
                
    mysqli_stmt_bind_result($stmt8$visitorUsername);

                
    // Fetch below in loop...

            
    }else{
                
    // Visitors Not Found.
                // Do Nothing...

            


    And then here is the loop I am concerned about...
    PHP Code:
            // Loop through Friends.
            
    while (mysqli_stmt_fetch($stmt8)){
                
    //Some code here...

            


    I am worried that if my query (i.e. mysqli_stmt_fetch($stmt8))) returns "0" or "NULL" that my loop could go crazy?!

    For a preliminary test, I changed my query to LIMIT 0, and my loop seemed to be okay, but I still feel like something is missing...

    Suggestions?

    Thanks,


    Debbie

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    http://www.php.net/manual/en/mysqli-stmt.fetch.php
    Code:
    Return Values 
    
    Value    Description
    TRUE     Success. Data has been fetched
    FALSE    Error occurred
    NULL     No more rows/data exists or data truncation occurred
    That NULL isn't the value of the column of your query, it's the value the function returns and depends only on the exit of the fetch. If it fetches a row with a column value of NULL, it will return TRUE. It's $visitorUsername that will contain the column value NULL.

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,934
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    http://www.php.net/manual/en/mysqli-stmt.fetch.php
    Code:
    Return Values 
    
    Value    Description
    TRUE     Success. Data has been fetched
    FALSE    Error occurred
    NULL     No more rows/data exists or data truncation occurred
    That NULL isn't the value of the column of your query, it's the value the function returns and depends only on the exit of the fetch. If it fetches a row with a column value of NULL, it will return TRUE. It's $visitorUsername that will contain the column value NULL.
    I saw that, but isn't the question really about my WHILE??

    (From what the Manual says, if the expression inside a WHILE is not TRUE, then the loop doesn't run.

    So that would imply that if mysqli_stmt_fetch($stmt8) returned a FALSE or NULL, then my loop simply wouldn't run.

    Right?


    Debbie

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    So that would imply that if mysqli_stmt_fetch($stmt8) returned a FALSE or NULL, then my loop simply wouldn't run.

    Right?
    Right.


Bookmarks

Posting Permissions

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