gpt4 book ai didi

php - 代码点火器错误 : Fatal error: Cannot use object of type CI_DB_mysql_result as array

转载 作者:行者123 更新时间:2023-12-02 17:41:30 25 4
gpt4 key购买 nike

我正在尝试为树实现一个 php codeigniter 模型,每次运行我的 Controller 时,我都会收到以下错误:

Fatal error: Cannot use object of type CI_DB_mysql_result as array in C:\AppServ\www\tree\application\models\Btree.php on line 50

我试图在行中修复这个语法

$currentID = $row['id'];

但是,仍然收到相同的错误消息。

我的模型函数是:

public function fetchTree($parentArray, $parentID = null)
{
// Create the query
if ($parentID == null)
$parentID = -1;

$sql = "SELECT `id` FROM `{$this->tblName}` WHERE `id`= ". intval($parentID);

// Execute the query and go through the results.
$result = $this->db->query($sql);
if ($result)
{
while ($row = $result)
{
// Create a child array for the current ID
$currentID = $row['id'];
$parentArray[$currentID] = array();

// Print all children of the current ID
$this->fetchTree($parentArray[$currentID], $currentID);
}
$result->close();
}
}

最佳答案

你的问题出在这一行:

while($row = $result)

您正在将 $row 设置为整个查询对象。您想要遍历查询的结果

试试这个:

$query = $this->db->query($sql);
foreach($query->result_array() AS $row) {
$currentID = $row['id'];
...

关于php - 代码点火器错误 : Fatal error: Cannot use object of type CI_DB_mysql_result as array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20022662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com