0

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>';
          }
        }
        ?>
Amit Sharma
  • 1,539
  • 2
  • 7
  • 17
brazencore
  • 17
  • 7

2 Answers2

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.

Amit Sharma
  • 1,539
  • 2
  • 7
  • 17
  • I am very much lost on how to install the mysqlnd driver to host gator cpanel. I looked at the post you anchored and Hostgator does not have anything similar to that. – brazencore Jan 09 '20 at 06:58
  • you need to contact your hosting provide. they will do it for yu – Amit Sharma Jan 09 '20 at 07:04
  • I think here you can find that info using phpinfo() function https://prnt.sc/ql5sj2 – Amit Sharma Jan 09 '20 at 07:09
  • I will try to contact my hosting provider tomorrow in case the bottom solution does not work. Thank you for the help and the external links. – brazencore Jan 09 '20 at 07:22
0

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.

Bartosz Pachołek
  • 1,148
  • 1
  • 6
  • 16
  • the first error has went away. Now it is giving me a "Fatal error : Allowed memory size of 268435456 bytes exhausted (tried to allocate 4294967296 bytes)" Which is a new question it self so I will do some reading on that and if I still can not figure it out I will ask a new question. Thank you for the help – brazencore Jan 09 '20 at 07:21
  • for allowed memory size issue you can check https://stackoverflow.com/questions/59654795/allowed-memory-size-php-ini/59655300#59655300 – Amit Sharma Jan 09 '20 at 07:28