#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0

    Question Wrong Number Shown As Result Found


    Php Buddies,

    A great mystery that is being solved in a failure for 2 nights now! See, if you have a better eye than me to solve the issue or not.
    It's a pagination script.
    I have the tbl with 9 entries from 4 usernames.
    When I want the pagination page to show all results (all usernames' inputs) I call the results via url like this:
    301 Moved Permanently

    Note the:
    ?browsing_history_owner_username=all

    When I want to pull results from a particular Username (eg. admin123), I call the results via url like this:
    301 Moved Permanently
    Note the:
    ?browsing_history_owner_username=admin123

    Now, since the tbl has 5 entries from user "admin123" and since I have set it to show 2 results per page then the pagination shows 3 pages of results. At the top of the page, it says "5 results found". So far, so good.

    But, when I try seeing all Usernames' results then I should be shown a total of 9 results in 5 pages. But guess what ? I am only shown 2 results (rows) but at the top of the page it says "0 results found"! Why is that ?
    Have a look at the imgs.

    PHP Code:
    <?php 

    //Required PHP Files. 
    include 'config.php'
    include 
    'header.php'
    include 
    'account_header.php'

    if (!
    $conn

        
    $error mysqli_connect_error(); 
        
    $errno mysqli_connect_errno(); 
        print 
    "$errno$error\n"
        exit(); 

    else 
    {     
        
    $max_result 100
        
    $links_per_page 2
        
        
    //Get the Page Number. Default is 1 (First Page). 
        
    $page_number $_GET["page_number"]; 
        if (
    $page_number == ""
        { 
            
    $page_number 1
        } 
        
        
    //Get the Username of the person who's Browsing History needs to be shown. 
        
    $browsing_history_owner_username $_GET["browsing_history_owner_username"]; 
        
    //If no username is present on the $_GET then set to "All" to show Browsing Histories of all Usernames. 
        
    if ($browsing_history_owner_username == ""
        { 
            
    $browsing_history_owner_username "all"
            
    $query_1 "SELECT COUNT(*) FROM browsing_histories"
            
    $stmt_1 mysqli_prepare($conn,$query_1); 
        }
        else 
        { 
            
    $query_1 "SELECT COUNT(*) FROM browsing_histories WHERE username = ?"
            
    $stmt_1 mysqli_prepare($conn,$query_1); 
            
    mysqli_stmt_bind_param($stmt_1,'s',$browsing_history_owner_username ); 
        } 
        
    mysqli_stmt_execute($stmt_1); 
        
    $result_1 mysqli_stmt_bind_result($stmt_1,$matching_rows_count); 
        
    mysqli_stmt_fetch($stmt_1); 
        
    mysqli_stmt_free_result($stmt_1); 
        
        
    $total_pages ceil($matching_rows_count/$links_per_page); 
        
    $offset = ($page_number-1)*$links_per_page//$offset = ($page_number*$links_per_page)-$links_per_page; 
        
        
    if ($browsing_history_owner_username == "all"
        { 
            
    $query_2 "SELECT * FROM browsing_histories ORDER BY id LIMIT ? OFFSET ?"
            
    //$query_2 = "SELECT id,date_and_time,browsed_page,referreral_page,username,contact_page,gender,date_of_birth,age_range,religion,education,profession,marital_status,working_status,home_town,home_borough,home_city,home_county,home_region,home_state,home_country FROM browsing_histories ORDER BY id LIMIT ? OFFSET ?"; 
            
    $stmt_2 mysqli_prepare($conn,$query_2); 
            
    mysqli_stmt_bind_param($stmt_2,'ii',$links_per_page,$offset); 
        } 
        else 
        { 
            
    $query_2 "SELECT * FROM browsing_histories WHERE username = ? ORDER BY id LIMIT ? OFFSET ?"
            
    //$query_2 = "SELECT id,date_and_time,browsed_page,referreral_page,username,contact_page,gender,date_of_birth,age_range,religion,education,profession,marital_status,working_status,home_town,home_borough,home_city,home_county,home_region,home_state,home_country FROM browsing_histories WHERE username = ? ORDER BY id LIMIT ? OFFSET ?"; 
            
    $stmt_2 mysqli_prepare($conn,$query_2); 
            
    mysqli_stmt_bind_param($stmt_2,'sii',$browsing_history_owner_username,$links_per_page,$offset); 
        } 
        
    mysqli_stmt_execute($stmt_2); 
        
    $result_2 mysqli_stmt_bind_result($stmt_2,$id,$date_and_time,$browsed_page,$referral_page,$username,$contact_page,$gender,$date_of_birth,$age_range,$religion,$education,$profession,$marital_status,$working_status,$home_town,$home_borough,$home_city,$home_county,$home_region,$home_state,$home_country); 
        
    mysqli_stmt_fetch($stmt_2); 
        
    ?> 
        
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> 
        <html> 
        <head> 
        <meta content="text/html; charset=ISO-8859-1" http-equiv=" content-type"> 
        <title><?php echo "$site_name User \"$browsing_history_owner_username\" Browsing Histories in $server_time time."?></title> 
        </head> 
        <body> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User \"$browsing_history_owner_username\" Browsing Histories in $server_time time."?> </span></align> 
        <br> 
        <br> 
        <table width="1500" border="0" cellpadding="5" cellspacing="2" bgcolor="#666666"> 
        <?php if(!$stmt_2
        { 
            
    ?> 
            <tr> 
            <td bgcolor="#FFFFFF">No record found! Try another time.</td> 
            </tr> 
            <?php 
        

        else 
        { 
            if((
    $offset+1)<=$max_result
            { 
                
    printf("<b> %d Result Found ...</b>\n",$matching_rows_count); ?><br> 
                <br> 
                <tr name="headings"> 
                <td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission Number</td> 
                <td bgcolor="#FFFFFF" name="column-heading_logging-server-date-and-time">Date & Time in <?php echo "$server_time?></td> 
                <td bgcolor="#FFFFFF" name="column-heading_browsed-page">Browsed Page</td> 
                <td bgcolor="#FFFFFF" name="column-heading_referral-page">Referral Page</td> 
                <td bgcolor="#FFFFFF" name="column-heading_username">Username</td> 
                <td bgcolor="#FFFFFF" name="column-heading_contact-page">Contact Page</td> 
                <td bgcolor="#FFFFFF" name="column-heading_gender">Gender</td> 
                <td bgcolor="#FFFFFF" name="column-heading_date-of-birth">Date Of Birth</td> 
                <td bgcolor="#FFFFFF" name="column-heading_age-range">Age Range</td> 
                <td bgcolor="#FFFFFF" name="column-heading_religion">Religion</td> 
                <td bgcolor="#FFFFFF" name="column-heading_education">Education</td> 
                <td bgcolor="#FFFFFF" name="column-heading_profession">Profession</td> 
                <td bgcolor="#FFFFFF" name="column-heading_marital-status">Marital Status</td> 
                <td bgcolor="#FFFFFF" name="column-heading_working-status">Working Status</td> 
                <td bgcolor="#FFFFFF" name="column-heading_home-town">Home Town</td> 
                <td bgcolor="#FFFFFF" name="column-heading_home-city">Home City</td> 
                <td bgcolor="#FFFFFF" name="column-heading_home-borough">Home Borough</td> 
                <td bgcolor="#FFFFFF" name="column-heading_home-county">Home County</td> 
                <td bgcolor="#FFFFFF" name="column-heading_home-region">Home Region</td> 
                <td bgcolor="#FFFFFF" name="column-heading_home-state">Home State</td> 
                <td bgcolor="#FFFFFF" name="column-heading_home-country">Home Country</td> 
                </tr> 
                <tr name="user-details">             
                <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                <td bgcolor="#FFFFFF" name="browsed-page"><?php printf("%s",$browsed_page); ?></td> 
                <td bgcolor="#FFFFFF" name="referral-page"><?php printf("%s",$referral_page); ?></td> 
                <td bgcolor="#FFFFFF" name="username"><?php printf("%s",$username); ?></td> 
                <td bgcolor="#FFFFFF" name="contact-page"><?php printf("%s",$contact_page); ?></td> 
                <td bgcolor="#FFFFFF" name="gender"><?php printf("%s",$gender); ?></td> 
                <td bgcolor="#FFFFFF" name="date-of-birth"><?php printf("%s",$date_of_birth); ?></td> 
                <td bgcolor="#FFFFFF" name="age-range"><?php printf("%s",$age_range); ?></td> 
                <td bgcolor="#FFFFFF" name="religion"><?php printf("%s",$religion); ?></td> 
                <td bgcolor="#FFFFFF" name="education"><?php printf("%s",$education); ?></td> 
                <td bgcolor="#FFFFFF" name="profession"><?php printf("%s",$profession); ?></td> 
                <td bgcolor="#FFFFFF" name="marital-status"><?php printf("%s",$marital_status); ?></td> 
                <td bgcolor="#FFFFFF" name="working-status"><?php printf("%s",$working_status); ?></td> 
                <td bgcolor="#FFFFFF" name="home_town"><?php printf("%s",$home_town); ?></td> 
                <td bgcolor="#FFFFFF" name="home-borough"><?php printf("%s",$home_borough); ?></td> 
                <td bgcolor="#FFFFFF" name="home-city"><?php printf("%s",$home_city); ?></td>             
                <td bgcolor="#FFFFFF" name="home-county"><?php printf("%s",$home_county); ?></td> 
                <td bgcolor="#FFFFFF" name="home-region"><?php printf("%s",$home_region); ?></td> 
                <td bgcolor="#FFFFFF" name="home-state"><?php printf("%s",$home_state); ?></td> 
                <td bgcolor="#FFFFFF" name="home_country"><?php printf("%s",$home_country); ?></td> 
                </tr> 
                <?php 
                
    //Use this technique: http://php.net/manual/en/mysqli-stmt.fetch.php 
                
    while(mysqli_stmt_fetch($stmt_2)) 
                { 
                    
    ?> 
                    <tr name="user-details">             
                    <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                    <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                    <td bgcolor="#FFFFFF" name="browsed-page"><?php printf("%s",$browsed_page); ?></td> 
                    <td bgcolor="#FFFFFF" name="referral-page"><?php printf("%s",$referral_page); ?></td> 
                    <td bgcolor="#FFFFFF" name="username"><?php printf("%s",$username); ?></td> 
                    <td bgcolor="#FFFFFF" name="contact-page"><?php printf("%s",$contact_page); ?></td> 
                    <td bgcolor="#FFFFFF" name="gender"><?php printf("%s",$gender); ?></td> 
                    <td bgcolor="#FFFFFF" name="date-of-birth"><?php printf("%s",$date_of_birth); ?></td> 
                    <td bgcolor="#FFFFFF" name="age-range"><?php printf("%s",$age_range); ?></td> 
                    <td bgcolor="#FFFFFF" name="religion"><?php printf("%s",$religion); ?></td> 
                    <td bgcolor="#FFFFFF" name="education"><?php printf("%s",$education); ?></td> 
                    <td bgcolor="#FFFFFF" name="profession"><?php printf("%s",$profession); ?></td> 
                    <td bgcolor="#FFFFFF" name="marital-status"><?php printf("%s",$marital_status); ?></td> 
                    <td bgcolor="#FFFFFF" name="working-status"><?php printf("%s",$working_status); ?></td> 
                    <td bgcolor="#FFFFFF" name="home_town"><?php printf("%s",$home_town); ?></td> 
                    <td bgcolor="#FFFFFF" name="home-borough"><?php printf("%s",$home_borough); ?></td> 
                    <td bgcolor="#FFFFFF" name="home-city"><?php printf("%s",$home_city); ?></td>             
                    <td bgcolor="#FFFFFF" name="home-county"><?php printf("%s",$home_county); ?></td> 
                    <td bgcolor="#FFFFFF" name="home-region"><?php printf("%s",$home_region); ?></td> 
                    <td bgcolor="#FFFFFF" name="home-state"><?php printf("%s",$home_state); ?></td> 
                    <td bgcolor="#FFFFFF" name="home-country"><?php printf("%s",$home_country); ?></td> 
                    </tr> 
                    <?php 
                    ?>
     
                    <tr name="pagination"> 
                    <td colspan="21" bgcolor="#FFFFFF"> Result Pages:             
                    <?php              
                    
    if($page_number<$total_pages
                    { 
                        for(
    $i=1;$i<=$total_pages;$i++) //Show Page Numbers in Serial Order. Eg. 1,2,3.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?browsing_history_owner_username=$browsing_history_owner_username&page_number={$i}\">{$i}</a> "
                    }             
                    else 
                    { 
                        for(
    $i=$total_pages;$i>=1;$i--) //Show Page Numbers in Reverse Order. Eg. 3,2,1.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?browsing_history_owner_username=$browsing_history_owner_username&page_number={$i}\">{$i}</a> "
                    } 
                    
    ?> 
                    </td> 
                    </tr> 
                    <?php 
                

            } 
        }
        
    ?>   
        </table>     
        <br> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User \"$browsing_history_owner_username\" Browsing Histories in $server_time time."?> </span></align> 
        <br> 
        </div> 
        <br> 
        </body> 
        </html> 
        <?php 
        
    //Free Result Set. 
        
    mysqli_stmt_free_result($stmt_2); 
        
    //Close Statement Connection. 
        
    mysqli_stmt_close($stmt_2); 
        
    //Close Database Connection. 
        
    mysqli_close($conn); 

    ?>
    Note line 93:
    PHP Code:
    printf("<b> %d Result Found ...</b>\n",$matching_rows_count); ?><br> 
    Note that, there is a relationship between line 93 and line 43.
    Line 43 is:
    PHP Code:
    $result_1 mysqli_stmt_bind_result($stmt_1,$matching_rows_count); 
    As you can see, $result_1 and $matching_rows_count should hold the same value. But they don't.
    On line 93, if I switch this:
    PHP Code:
    printf("<b> %d Result Found ...</b>\n",$matching_rows_count); ?><br> 
    to this in line 93:

    PHP Code:
    printf("<b> %d Result Found ...</b>\n",$result_1); ?><br> 
    then I get shown different result at the top of the page (Result Found ...). That is another mystery! Why is the result changing since both $result_1 and $matching_rows_count are supposed to show the same result ?
    Result_5.png
    Result_0.png
    Result_Mystery.png
    Last edited by UniqueIdeaMan; April 30th, 2018 at 12:25 PM.
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,896
    Rep Power
    9646

IMN logo majestic logo threadwatch logo seochat tools logo