PHP警告mysqli_fetch_assoc() [英] PHP Warning mysqli_fetch_assoc()
问题描述
PHP版本:7.1我一直在关注mmtuts PHP视频,了解如何为用户构建上传选项.当我跟踪视频时,他编写的代码在localhost(通过xampp的数据库(mysql))上运行良好,但是随后当我去HostGator将其连接到实际数据库时.我收到错误消息
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
致命错误:未捕获的错误:调用gallery.phponline45上未定义的函数mysqli_stmt_get_result()
Fatal error : Uncaught Error: Call to undefined function mysqli_stmt_get_result() in gallery.phpon line45
我在此网站上查找了错误,大部分答案都被用来检查我的PHP扩展,但是我无法在PHP.INI的Hostgator控制面板中找到mysqlnd和nd_mysqli扩展.如果您能指导我通过,将不胜感激.但是,其他答案也表明我可以使用bind and fetch方法,即使在研究了如何将此代码转换为bind and fetch方法之后,我还是真的不理解它.我对PHP完全陌生(尝试学习的第7天),所以我正在寻求有关如何解决此问题的建议.预先谢谢你.
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>';
}
}
?>
推荐答案
函数 mysqli_stmt_get_result
依赖于仅在使用特定适配器时才可用的函数(mysql本机驱动程序[实际上更好,但是一些托管服务提供商不喜欢它的某些功能,或者只是提供非常古老的php ...]而不是libmysqlclient.)
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).
最好的选择是从学习 PDO
开始,但是如果您想坚持使用 mysqli
,那么我也鼓励使用面向对象的样式-一个很好的例子您可以在这里找到: https://www.php.net/manual/en/mysqli-stmt.execute.php
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
但仅用于修复文件:
0)(使用那个旧的libmysqlclient)的主要问题是,您需要确切知道将拥有多少列-我看到您想要三列:-imgFullNameGallery-title画廊-descGallery
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;";
收件人:
$sql = "SELECT imgFullNameGallery, titleGallery, descGallery FROM gallery ORDER BY orderGallery DESC;";
1)替换行
$result = mysqli_stmt_get_result($stmt);
具有:
mysqli_stmt_bind_result($stmt, $imgFullNameGallery, $titleGallery, $descGallery);
现在替换:
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>';
}
具有:
while (mysqli_stmt_fetch($stmt)) {
echo '<a href="#">
<div style="background-image: url(img/gallery/'.$imgFullNameGallery.');"></div>
<h3>'.$titleGallery.'</h3>
<p>'.$descGallery.'</p>
</a>';
}
如您所见,每次调用 fetch
都会填充绑定的变量 $ imgFullNameGallery
, $ titleGallery
和 $ descGallery 代码>及其各自在数据库行中的值.(变量实际上可以用不同的名称命名,没关系,只是它们的计数很重要)
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);
和连接:
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.
这篇关于PHP警告mysqli_fetch_assoc()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!