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

    Join Date
    Jan 2017
    Posts
    830
    Rep Power
    0

    Why MySqli Get Result & Fetch Result All Fail ?


    Php Folks,

    On my following codes, the "$stmt->bind_result" (sample 1) code works.

    But, sample 2 & 3 "mysqli_stmt_get_result($stmt)" does not work. Why is that ? I see a complete white blank page. Tbl has atleast 6 entries.

    And, sample 4 "mysqli_stmt_fetch($stmt)" only shows result from one column "date_and_time". Why is that ? I need it to show results from all columns.

    And, sample 5 "mysqli_stmt_fetch($stmt)" shows blank page.


    SAMPLE 1: BIND RESULT WORKING
    PHP Code:

    <?php 

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

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
    if (login_check() === FALSE
    {
        
    //Redirect User to Log-in Page after 2 secs. 
        
    header("refresh:2; url=login.php"); 
        exit(); 

    else 

        
    $user $_SESSION["user"]; 
        
        
    $id $_SESSION["id"]; 
        
    $account_activation_status $_SESSION["account_activation_status"]; 
        
    $id_video_verification_status $_SESSION["id_video_verification_status"]; 
        
    $id_video_verification_url $_SESSION["id_video_verification_url"]; 
        
    $sponsor_username $_SESSION["sponsor_username"]; 
        
    $recruits_number $_SESSION["recruits_number"]; 
        
    $on_day_number_on_7_days_wish_list $_SESSION["on_day_number_on_7_days_wish_list"]; 
        
    $primary_website_domain $_SESSION["primary_website_domain"]; 
        
    $primary_website_email $_SESSION["primary_website_email"]; 
        
    $username $_SESSION["username"]; 
        
    $first_name $_SESSION["first_name"]; 
        
    $middle_name $_SESSION["middle_name"]; 
        
    $surname $_SESSION["surname"]; 
        
    $gender $_SESSION["gender"]; 
        
    $age_range $_SESSION["age_range"]; 
        
    $religion $_SESSION["religion"]; 
        
    $marital_status $_SESSION["marital_status"]; 
        
    $working_status $_SESSION["working_status"]; 
        
    $profession $_SESSION["profession"]; 
        
        
    $recipient_username $user;

        
    ?> 
        <!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 $user ?>Notices in <?php $server_time ?> time.</title>
        </head> 
        <body> 
        <br> 
        <center><span style="font-weight: bold;"><?php $user ?>Notices in <?php $server_time ?> time.</span></center> 
        <br> 
        <br> 
        
    <?php 

    $recipient_username 
    $user
    $query 'SELECT id, date_and_time, recipient_username, sender_username, message FROM notices WHERE recipient_username = ?';

    if(
    $stmt $conn->prepare($query)){
       
    /*
            Binds variables to prepared statement

            i    corresponding variable has type integer
            d    corresponding variable has type double
            s    corresponding variable has type string
            b    corresponding variable is a blob and will be sent in packets
    */
       
    $stmt->bind_param('i',$id);

       
    /* execute query */
       
    $stmt->execute();

       
    /* Store the result (to get properties) */
       
    $stmt->store_result();

       
    /* Get the number of rows */
       
    $num_of_rows $stmt->num_rows;

       
    /* Bind the result to variables */
       
    $stmt->bind_result($id$date_and_time$recipient_username$sender_username$message);

       while (
    $stmt->fetch()) {
            echo 
    'ID: '.$id.'<br>';
            echo 
    'To: '.$recipient_username.'<br>';
            echo 
    'From: '.$sender_username.'<br>';
            echo 
    'Mssg: '.$message.'<br><br>';
       }

       
    /* free results */
       
    $stmt->free_result();

       
    /* close statement */
       
    $stmt->close();
    }
    }
    ?>

    SAMPLE 2: GET RESULT NOT WORKING AS SHOWS BLANK PAGE
    PHP Code:


    <?php 

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

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
    if (login_check() === FALSE
    {
        
    //Redirect User to Log-in Page after 2 secs. 
        
    header("refresh:2; url=login.php"); 
        exit(); 

    else 

        
    $user $_SESSION["user"]; 
        
        
    $id $_SESSION["id"]; 
        
    $account_activation_status $_SESSION["account_activation_status"]; 
        
    $id_video_verification_status $_SESSION["id_video_verification_status"]; 
        
    $id_video_verification_url $_SESSION["id_video_verification_url"]; 
        
    $sponsor_username $_SESSION["sponsor_username"]; 
        
    $recruits_number $_SESSION["recruits_number"]; 
        
    $on_day_number_on_7_days_wish_list $_SESSION["on_day_number_on_7_days_wish_list"]; 
        
    $primary_website_domain $_SESSION["primary_website_domain"]; 
        
    $primary_website_email $_SESSION["primary_website_email"]; 
        
    $username $_SESSION["username"]; 
        
    $first_name $_SESSION["first_name"]; 
        
    $middle_name $_SESSION["middle_name"]; 
        
    $surname $_SESSION["surname"]; 
        
    $gender $_SESSION["gender"]; 
        
    $age_range $_SESSION["age_range"]; 
        
    $religion $_SESSION["religion"]; 
        
    $marital_status $_SESSION["marital_status"]; 
        
    $working_status $_SESSION["working_status"]; 
        
    $profession $_SESSION["profession"]; 
        
        
    $recipient_username $user;

        
    ?> 
        <!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 $user ?>Notices in <?php $server_time ?> time.</title>
        </head> 
        <body> 
        <br> 
        <center><span style="font-weight: bold;"><?php $user ?>Notices in <?php $server_time ?> time.</span></center> 
        <br> 
        <br> 
        
    <?php 

    if (!$conn)
    {
        
    $error mysqli_connect_error();
        
    $errno mysqli_connect_errno();
        print 
    "$errno$error\n";
        exit();
    }

    $recipient_username $user
    $query "SELECT id, date_and_time, sender_username, message FROM notices WHERE recipient_username = ? ORDER BY recipient_username LIMIT 10";

    $stmt mysqli_stmt_init($conn);
    if(!
    mysqli_stmt_prepare($stmt$query))
    {
        print 
    "Failed to prepare statement\n";
    }
    else
    {
        
    mysqli_stmt_bind_param($stmt"s"$id);

        
    $notices_array = array('id','recipient_username','sender_username','message');

        foreach(
    $notices_array as $notice)
        {
            
    mysqli_stmt_execute($stmt);
            
    $result mysqli_stmt_get_result($stmt);
            while (
    $row mysqli_fetch_array($resultMYSQLI_NUM))
            {
                foreach (
    $row as $r)
                {
                    print 
    "$r ";
                }
                print 
    "\n";
            }
        }
    }
    mysqli_stmt_close($stmt);
    mysqli_close($conn);

    }
    }
    ?>

    SAMPLE 3: GET RESULT NOT WORKING AS SHOWS BLANK PAGE
    PHP Code:

    <?php 

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

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
    if (login_check() === FALSE
    {
        
    //Redirect User to Log-in Page after 2 secs. 
        
    header("refresh:2; url=login.php"); 
        exit(); 

    else 

        
    $user $_SESSION["user"]; 
        
        
    $id $_SESSION["id"]; 
        
    $account_activation_status $_SESSION["account_activation_status"]; 
        
    $id_video_verification_status $_SESSION["id_video_verification_status"]; 
        
    $id_video_verification_url $_SESSION["id_video_verification_url"]; 
        
    $sponsor_username $_SESSION["sponsor_username"]; 
        
    $recruits_number $_SESSION["recruits_number"]; 
        
    $on_day_number_on_7_days_wish_list $_SESSION["on_day_number_on_7_days_wish_list"]; 
        
    $primary_website_domain $_SESSION["primary_website_domain"]; 
        
    $primary_website_email $_SESSION["primary_website_email"]; 
        
    $username $_SESSION["username"]; 
        
    $first_name $_SESSION["first_name"]; 
        
    $middle_name $_SESSION["middle_name"]; 
        
    $surname $_SESSION["surname"]; 
        
    $gender $_SESSION["gender"]; 
        
    $age_range $_SESSION["age_range"]; 
        
    $religion $_SESSION["religion"]; 
        
    $marital_status $_SESSION["marital_status"]; 
        
    $working_status $_SESSION["working_status"]; 
        
    $profession $_SESSION["profession"]; 
        
        
    $recipient_username $user;

        
    ?> 
        <!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 $user ?>Notices in <?php $server_time ?> time.</title>
        </head> 
        <body> 
        <br> 
        <center><span style="font-weight: bold;"><?php $user ?>Notices in <?php $server_time ?> time.</span></center> 
        <br> 
        <br> 
        
    <?php 


    $id 
    $_SESSION["id"]; 
    //$query = 'SELECT id, date_and_time, recipient_username, sender_username, message FROM notices WHERE id = ?';

    $query 'SELECT * FROM notices WHERE id = ?'


    if(
    $stmt $conn->prepare($query)){
     
    /*
        Binds variables to prepared statement

        i    corresponding variable has type integer
        d    corresponding variable has type double
        s    corresponding variable has type string
        b    corresponding variable is a blob and will be sent in packets
     */
    $stmt->bind_param('i',$id);

    /* execute query */
    $stmt->execute();

    /* Get the result */
    $result $stmt->get_result();

    /* Get the number of rows */
    $num_of_rows $result->num_rows;

    while (
    $row $result->fetch_assoc()) {
            echo 
    'ID: '.$id.'<br>';
            echo 
    'To: '.$recipient_username.'<br>';
            echo 
    'From: '.$sender_username.'<br>';
            echo 
    'Mssg: '.$message.'<br><br>';
    }

    /* free results */
    $stmt->free_result();

    }
    }
    ?>

    SAMPLE 4: FETCH RESULT NOT WORKING AS ONLY SHOWS DATE & TIME COLUMN DATA:
    28 2018-02-21 20:58:49 29 2018-02-21 21:01:36 30 2018-02-21 21:02:17 31 2018-02-21 21:03:40 32 2018-02-21 21:13:20
    PHP Code:
    <?php 

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

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
    if (login_check() === FALSE
    {
        
    //Redirect User to Log-in Page after 2 secs. 
        
    header("refresh:2; url=login.php"); 
        exit(); 

    else 

        
    $user $_SESSION["user"]; 
        
        
    $id $_SESSION["id"]; 
        
    $account_activation_status $_SESSION["account_activation_status"]; 
        
    $id_video_verification_status $_SESSION["id_video_verification_status"]; 
        
    $id_video_verification_url $_SESSION["id_video_verification_url"]; 
        
    $sponsor_username $_SESSION["sponsor_username"]; 
        
    $recruits_number $_SESSION["recruits_number"]; 
        
    $on_day_number_on_7_days_wish_list $_SESSION["on_day_number_on_7_days_wish_list"]; 
        
    $primary_website_domain $_SESSION["primary_website_domain"]; 
        
    $primary_website_email $_SESSION["primary_website_email"]; 
        
    $username $_SESSION["username"]; 
        
    $first_name $_SESSION["first_name"]; 
        
    $middle_name $_SESSION["middle_name"]; 
        
    $surname $_SESSION["surname"]; 
        
    $gender $_SESSION["gender"]; 
        
    $age_range $_SESSION["age_range"]; 
        
    $religion $_SESSION["religion"]; 
        
    $marital_status $_SESSION["marital_status"]; 
        
    $working_status $_SESSION["working_status"]; 
        
    $profession $_SESSION["profession"]; 
        
        
    $recipient_username $user;

        
    ?> 
        <!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 $user ?>Notices in <?php $server_time ?> time.</title>
        </head> 
        <body> 
        <br> 
        <center><span style="font-weight: bold;"><?php $user ?>Notices in <?php $server_time ?> time.</span></center> 
        <br> 
        <br> 
        
    <?php 

    /* check connection */
    if (!$conn) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }

    $id $_SESSION["id"]; 
    /* prepare statement */
    if ($stmt mysqli_prepare($conn"SELECT id, date_and_time, recipient_username, sender_username, message FROM notices ORDER BY id LIMIT 5")) {
        
    mysqli_stmt_execute($stmt);

        
    /* bind variables to prepared statement */
        
    mysqli_stmt_bind_result($stmt$id$date_and_time$recipient_username$sender_username$message);

        
    /* fetch values */
        
    while (mysqli_stmt_fetch($stmt)) {
            
    printf("%s %s\n"$id$date_and_time$recipient_username$sender_username$message);
        }

        
    /* close statement */
        
    mysqli_stmt_close($stmt);
    }

    /* close connection */
    mysqli_close($conn);
    }

    ?>

    SAMPLE 5: FETCH RESULT NOT WORKING AS SHOWS BLANK PAGE

    PHP Code:
    <?php 

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

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
    if (login_check() === FALSE
    {
        
    //Redirect User to Log-in Page after 2 secs. 
        
    header("refresh:2; url=login.php"); 
        exit(); 

    else 

        
    $user $_SESSION["user"]; 
        
        
    $id $_SESSION["id"]; 
        
    $account_activation_status $_SESSION["account_activation_status"]; 
        
    $id_video_verification_status $_SESSION["id_video_verification_status"]; 
        
    $id_video_verification_url $_SESSION["id_video_verification_url"]; 
        
    $sponsor_username $_SESSION["sponsor_username"]; 
        
    $recruits_number $_SESSION["recruits_number"]; 
        
    $on_day_number_on_7_days_wish_list $_SESSION["on_day_number_on_7_days_wish_list"]; 
        
    $primary_website_domain $_SESSION["primary_website_domain"]; 
        
    $primary_website_email $_SESSION["primary_website_email"]; 
        
    $username $_SESSION["username"]; 
        
    $first_name $_SESSION["first_name"]; 
        
    $middle_name $_SESSION["middle_name"]; 
        
    $surname $_SESSION["surname"]; 
        
    $gender $_SESSION["gender"]; 
        
    $age_range $_SESSION["age_range"]; 
        
    $religion $_SESSION["religion"]; 
        
    $marital_status $_SESSION["marital_status"]; 
        
    $working_status $_SESSION["working_status"]; 
        
    $profession $_SESSION["profession"]; 
        
        
    $recipient_username $user;

        
    ?> 
        <!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 $user ?>Notices in <?php $server_time ?> time.</title>
        </head> 
        <body> 
        <br> 
        <center><span style="font-weight: bold;"><?php $user ?>Notices in <?php $server_time ?> time.</span></center> 
        <br> 
        <br> 
        
    <?php 

    /* check connection */
    if (!$conn) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }

    $id $_SESSION["id"]; 

    $stmt mysqli_prepare($conn'SELECT id, date_and_time, recipient_username, sender_username, message FROM notices WHERE recipient_username=?');
    mysqli_stmt_bind_param($stmt's'$recipient_username);
    mysqli_stmt_execute($stmt);

    $notices_row = array();
    mysqli_stmt_bind_result($stmt$notices_row['id'], $notices_row['date_and_time'], $notices_row['recipient_username'], $notices_row['sender_username'], $notices_row['message']);
    while (
    mysqli_stmt_fetch($stmt)) 
    {
      echo 
    '<p>' $notices_row['id'] . '</p>';
      echo 
    '<p>' $notices_row['date_and_time'] . '</p>';
      echo 
    '<p>' $notices_row['recipient_username'] . '</p>';
      echo 
    '<p>' $notices_row['sender_username'] . '</p>';
      echo 
    '<p>' $notices_row['message'] . '</p>';
    }

    }

    ?>

    Ignore my column names if they seem silly. First things first, got to fix why the row results are not displaying on page for the get_result and fetch_result, before renaming columns.
    I'd appreciate it if anyone takes some of my sample codes (if not all) and edits it and shows where I went wrong by showing the edited sample(s). But, I prefer someone be kind enough to edit all samples.

    Thanks!
    Last edited by UniqueIdeaMan; February 23rd, 2018 at 10:43 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,451
    Rep Power
    652
    You really need to spend some time learning debugging techniques.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    830
    Rep Power
    0
    Originally Posted by gw1500se
    You really need to spend some time learning debugging techniques.
    Problem is, when you have error reportings on and get no errors but a complete blank page, then you really are in for a fix!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,451
    Rep Power
    652
    See previous post.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    136
    Rep Power
    65
    SAMPLE 2:
    This code is producing a blank page because it contains a fatal php parse/syntax error. There are to many }. You would know this if the error_reporting/display_errors settings were set in the php.ini on your development system.

    Even with this syntax error corrected, it's not very likely that $_SESSION['id'] contains a recipient_username, so the query would be matching zero rows because the WHERE clause is false.

    SAMPLE 3:
    This code is probably* running, but it's not very likely that $_SESSION['id'] contains a row's auto-increment id value, so the query would be matching zero rows because the WHERE clause is false.

    * - if the error_reporting/display_errors settings are not set to report and display all errors, the ->get_result() call could be producing a fatal runtime error if it is not present on your system.

    The code looping over the $notices_array is also nonsense and would be repeating the same output for each entry in that array.

    SAMPLE 4:
    The code is displaying what it was written to do. The printf() statement is only displaying two of the values. You would know this if you looked at the code that's responsible for producing the output and if you looked at the php.net documentation for the printf() statement.

    SAMPLE 5:
    This code is likely running, but it's not very likely that $recipient_username/$user/$_SESSION["user"] contains a value that matches the recipient_username column, so the query would be matching zero rows because the WHERE clause is false.

    For the cases where the code is running, but matching zero rows, you should always have program logic to test how many rows a query matches and output an appropriate message when it matches zero rows, i.e. get your code to tell you what it is doing, so that you don't need to guess.

    Comments on this post

    • UniqueIdeaMan agrees : Eye Opener!
    Last edited by DSmabismad; February 24th, 2018 at 08:37 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    830
    Rep Power
    0
    Originally Posted by DSmabismad
    This code is producing a blank page because it contains a fatal php parse/syntax error. There are to many }. You would know this if the error_reporting/display_errors settings were set in the php.ini on your development system.

    Even with this syntax error corrected, it's not very likely that $_SESSION['id'] contains a recipient_username, so the query would be matching zero rows because the WHERE clause is false.



    This code is probably* running, but it's not very likely that $_SESSION['id'] contains a row's auto-increment id value, so the query would be matching zero rows because the WHERE clause is false.

    * - if the error_reporting/display_errors settings are not set to report and display all errors, the ->get_result() call could be producing a fatal runtime error if it is not present on your system.

    The code looping over the $notices_array is also nonsense and would be repeating the same output for each entry in that array.



    The code is displaying what it was written to do. The printf() statement is only displaying two of the values. You would know this if you looked at the code that's responsible for producing the output and if you looked at the php.net documentation for the printf() statement.



    This code is likely running, but it's not very likely that $recipient_username/$user/$_SESSION["user"] contains a value that matches the recipient_username column, so the query would be matching zero rows because the WHERE clause is false.

    For the cases where the code is running, but matching zero rows, you should always have program logic to test how many rows a query matches and output an appropriate message when it matches zero rows, i.e. get your code to tell you what it is doing, so that you don't need to guess.
    Thanks man!
    I see I missed a column in the mysqli_prepare in sample 3.
    I fixed sample 4 few hrs ago regarding the printf. I spotted that eventually.
    As for the others, I'll need to check them out according to your replies.

    I managed to get one of these 6 samples to work using prep stmts procedural style. Struggling with the pagination part. Opening another thread about pagination. Look-out for it.

    Once again: CHEERS!
    Thanks

    EDIT: Error reporting is on.

    include 'error_reporting.php';

    That reference to the error reporting file is in config.php. Look at the top of all my 6 samples. It says config.php. I organized things a-bit so I don't have to add the error reporting code on all files.
    Last edited by UniqueIdeaMan; February 24th, 2018 at 09:07 AM.

IMN logo majestic logo threadwatch logo seochat tools logo