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
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.
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 withmysqli
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:
to:
1) replace the line
with:
Now replace:
with:
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:
and the connection:
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.