gpt4 book ai didi

php - CodeIgniter 错误为 foreach() 提供的参数无效

转载 作者:搜寻专家 更新时间:2023-10-30 21:47:15 25 4
gpt4 key购买 nike

我正在尝试从数据库中选择数据,但出现此错误:

  A PHP Error was encountered        
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/hello.php
Line Number: 24

Controller :

<?php
// system/application/controllers/hello.php

class Hello extends CI_Controller {

public function __construct()
{
parent::__construct();
}

function show_data() {

$query = $this->load->model('user');
foreach ($query->result() as $value) {
echo $value -> username;
echo $value -> password;
echo $value -> first_name;
echo $value -> last_name;
}
}
}
?>

型号:

class User extends CI_Model {

function __construct()
{
// Call the Model constructor
parent::__construct();
}

function show()
{
$query = $this->db->get('user', 10);
return $query->result();
}
}

我正在查看本教程:https://www.codeigniter.com/user_guide/database/results.html

错误是什么意思?

最佳答案

你的 Controller 函数应该是这样的,

function show_data() {
$this->load->model('user');
$query = $this->user->show();
foreach($query as $value) {
echo $value->username;
echo $value->password;
echo $value->first_name;
echo $value->last_name;
}
}

检查 here关于如何加载模型。

关于php - CodeIgniter 错误为 foreach() 提供的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14666813/

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