php mysqli fetch_all() 不是一个有效的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6694437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:00:56  来源:igfitidea点击:

mysqli fetch_all() not a valid function?

phpmysqli

提问by Phil

Thanks to the answers I have figured out that I am unable to use fetch_all()because i am using PHP 5.2.17- fetch_assocwith whileloop worked.

感谢这些答案,我发现我无法使用,fetch_all()因为我正在使用PHP 5.2.17- fetch_assocwithwhile循环有效。



The function I am using fetch_allis coming back with this error:

我正在使用的函数fetch_all返回此错误:

Fatal error: Call to undefined method mysqli_result::fetch_all() in

致命错误:调用未定义的方法 mysqli_result::fetch_all()

$mysqli = new mysqli($host, $username, $password, $database);
$query = "LONG QUERY that works, tested in phpmyadmin"
$result = $mysqli->query($query);
$result->fetch_all(); or  $mysqli->fetch_all() tried both
mysqli_fetch_all() was already tried.
$mysqli->close(); 

I am able to connect to the DB and I have pulled single rows. When I place the query in PHPMYADMIN I get 5 rows back.

我能够连接到数据库并且我已经拉了单行。当我将查询放在 PHPMYADMIN 中时,我得到 5 行。

Does this function even work? Is there a way I can place my data into an assoc array on my own?

这个功能还能用吗?有没有办法可以自己将数据放入关联数组中?

回答by Karolis

This function is available since PHP 5.3.0. Possibly your version is older. Use fetch_assoc()instead.

该函数自 PHP 5.3.0 起可用。可能你的版本比较旧。使用fetch_assoc()来代替。

while ($row = $result->fetch_assoc()) {
    // do what you need.
}

回答by Hassan Azimi

mysqli::fetch_all()needs mysqlnddriver to be installed before you can use it.

mysqli::fetch_all()需要mysqlnd安装驱动才能使用。

回答by Michael Berkowski

The typical way to construct an associative array is in a whileloop:

构造关联数组的典型方法是在while循环中:

$results_array = array();
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
  $results_array[] = $row;
}

回答by Fabrizio D'Ammassa

Please be careful about using fetch_all():

请小心使用 fetch_all():

http://www.php.net/manual/en/mysqli-result.fetch-all.php#88031

http://www.php.net/manual/en/mysqli-result.fetch-all.php#88031

Read the note about mysqldn requirement.

阅读有关 mysqldn 要求的说明。

回答by Sascha Galley

As an addition to Fabrizios answer, please consider building up your array with a loop:

作为 Fabrizios 答案的补充,请考虑使用循环构建数组:

$array = array();
while($row = $result->fetch_assoc())
    $array[] = $row;

回答by javatar

Issue seems to be 'mysqli' and 'mysqlnd' are not working together. I had the same issue in cpanel

问题似乎是 'mysqli' 和 'mysqlnd' 不能一起工作。我在 cpanel 中遇到了同样的问题

Steps:

脚步:

1) Go to CPanel dashboard

1) 转到 CPanel 仪表板

2) Go to 'Select PHP Version', you should see a bunch of checkboxes

2) 转到“选择 PHP 版本”,您应该会看到一堆复选框

3) Set PHP version (5.4 or above)

3)设置PHP版本(5.4或以上)

4) Uncheck the 'mysqli' extension and check both 'mysqlind' and 'nd_mysqli'

4) 取消选中“mysqli”扩展名并选中“mysqlind”和“nd_mysqli”

Apparently 'nd_mysqli' is 'mysqli' but configured to work with 'mysqlind' and to make it work you have to uncheck 'mysqli' due to settings conflicts.

显然 'nd_mysqli' 是 'mysqli',但配置为与 'mysqlind' 一起使用,为了使其工作,由于设置冲突,您必须取消选中 'mysqli'。

Following answer I found herehelped me.

我在这里找到的以下答案对我有帮助。

回答by Coco

I had 5.6 but didn't work for me anyway. By enabling the mysqlnd driver it worked for me. Just write apt-get install php5-mysqlndthen restart php and you're good to go!

我有 5.6 但无论如何都不适合我。通过启用 mysqlnd 驱动程序,它对我有用。只需编写apt-get install php5-mysqlnd然后重新启动 php 就可以了!

回答by Wilson

If you don't want to iterate using Karolis' answer, use

如果您不想使用Karolis的答案进行迭代,请使用

     while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
    {
       $rows[] = $row;
    }

to achieve the same result as

达到相同的结果

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

You may change constant to MYSQLI_NUMor MYSQLI_BOTH. This optional parameter are constants indicating what type of array should be produced from the current row data.

您可以将常量更改为MYSQLI_NUMMYSQLI_BOTH。此可选参数是常量,指示应从当前行数据生成什么类型​​的数组。

Notice that mysqli_fetch_array($result,MYSQLI_ASSOC )behaves identically to the mysqli_fetch_assoc()function, while mysqli_fetch_array($result,MYSQLI_NUM)will behave identically to the mysqli_fetch_row()function. The final option MYSQLI_BOTHwill create a single array with the attributes of both. http://php.net/manual/en/mysqli-result.fetch-array.php

请注意,mysqli_fetch_array($result,MYSQLI_ASSOC )mysqli_fetch_assoc()函数的行为相同,而mysqli_fetch_array($result,MYSQLI_NUM)将与mysqli_fetch_row()函数的行为相同。最后一个选项MYSQLI_BOTH将创建一个具有两者属性的数组。 http://php.net/manual/en/mysqli-result.fetch-array.php

回答by sirmagid

PHP Fatal error: Call to undefined function mysqli_fetch_all()

PHP 致命错误:调用未定义的函数 mysqli_fetch_all()

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);//not work

change to:php 5.3 or

更改为:php 5.3 或

$query = "SELECT * FROM `user_counter` ORDER BY id DESC LIMIT 20";
$result =  mysqli_query($connection, $query) or die
("Error in Selecting " . mysqli_error($connection));
$rows = array();
while ($row = $result->fetch_assoc()) {
    $rows[] = $row;
}
echo json_encode($rows);

回答by Chris Langtiw

Just to add an additional note about the mysqlnddriver: It also has to have the mysqliAPI extension installed. Without the extension, fetch_all()still won't be available. I found this while troubleshooting why my code worked on one server but not another, both PHP > 5.3. Use phpinfo()to verify the installed extensions.

只是添加有关mysqlnd驱动程序的附加说明:它还必须mysqli安装 API 扩展。没有扩展,fetch_all()仍然无法使用。我在解决为什么我的代码在一台服务器上运行而不是在另一台服务器上运行时发现了这一点,两者都是 PHP > 5.3。使用phpinfo()验证安装的扩展。