skip to Main Content

PHP VERSION: 7.1
I have been following mmtuts PHP videos on how to build an uploading option for users. As I was following the video, the code that he wrote works perfectly fine on the localhost (database (mysql) through xampp) but then when I went to HostGator to connect it to an actual database. I get the error saying

Fatal error
: Uncaught Error: Call to undefined function mysqli_stmt_get_result() in gallery.phpon line45

I looked up the error on this website, most of the answers said to check my PHP extensions but I am not being able to find the extensions mysqlnd and nd_mysqli in Hostgator control panel under PHP.INI. If you can guide me through that would be appreciated. However other answers also stated that I can use bind and fetch method and even after looking at how to do transform this code in to bind and fetch method, I am really not understanding it. I am completely new to PHP (7th day of trying to learn) so I am seeking advice on how to resolve this issue. Thank you in advance.

   <?php
        include_once 'includes/dbh.inc.php';

        $sql = "SELECT * FROM gallery ORDER BY orderGallery DESC;";

        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
          echo "SQL statement failed!";
        } else {

          mysqli_stmt_execute($stmt);
          $result = mysqli_stmt_get_result($stmt);

          while ($row = mysqli_fetch_assoc($result)) {
            echo '<a href="#">
              <div style="background-image: url(img/gallery/'.$row["imgFullNameGallery"].');"></div>
              <h3>'.$row["titleGallery"].'</h3>
              <p>'.$row["descGallery"].'</p>
            </a>';
          }
        }
        ?>

2

Answers


  1. I think your mysqlnd driver properly installed? As Per a comment in the Manual:

    If you are missing the mysqlnd installed/loaded or you do not have it installed whatever, you will get an undefined reference when trying to call “mysqli_stmt_get_result()”.

    Lastly, here‘s a forum discussing info pertaining to using the driver with cpanel that you may find useful.

    Login or Signup to reply.
  2. Function mysqli_stmt_get_result relies on a function which is available only when using a specific adapter (mysql native driver [which is actually better, but some hosting providers do not like some of its features or simply provide very old php…] instead of libmysqlclient).

    The best option would be to start by learning PDO, but if you want to stick with mysqli then I also encourage to use object-oriented style – a good example you can find here:
    https://www.php.net/manual/en/mysqli-stmt.execute.php

    But just to fix your file:

    0) The main issue (with that old libmysqlclient) is that you need to know exactly how many columns you will have – i see that you want three:
    – imgFullNameGallery
    – titleGallery
    – descGallery

    Then modify your query from:

    $sql = "SELECT * FROM gallery ORDER BY orderGallery DESC;";
    

    to:

    $sql = "SELECT imgFullNameGallery, titleGallery, descGallery FROM gallery ORDER BY orderGallery DESC;";
    

    1) replace the line

    $result = mysqli_stmt_get_result($stmt);
    

    with:

    mysqli_stmt_bind_result($stmt, $imgFullNameGallery, $titleGallery, $descGallery);
    

    Now replace:

    while ($row = mysqli_fetch_assoc($result)) {
                echo '<a href="#">
                  <div style="background-image: url(img/gallery/'.$row["imgFullNameGallery"].');"></div>
                  <h3>'.$row["titleGallery"].'</h3>
                  <p>'.$row["descGallery"].'</p>
                </a>';
              }
    

    with:

    while (mysqli_stmt_fetch($stmt)) {
        echo '<a href="#">
          <div style="background-image: url(img/gallery/'.$imgFullNameGallery.');"></div>
          <h3>'.$titleGallery.'</h3>
          <p>'.$descGallery.'</p>
        </a>';
    }
    

    as you can see, each call of fetch will fill bound variables $imgFullNameGallery, $titleGallery and $descGallery with their respective values from the row of the database. (variables can be actually named differently, that does not matter, just their count matters)

    Also below, in case you are not re-using it be sure to close the statment:

    mysqli_stmt_close($stmt);
    

    and the connection:

    mysqli_close($link);
    

    Let me know if that helps you. Also: the fact that you need to do it that way is only because of your hosting provider which limits your functionality.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search