致命错误:无法在第38行的C:\xampp\htdocs\ov400\application\views\Useraccount\Userlist.php中将stdClass类型的对象用作数组
I get error致命错误:无法将stdClass类型的对象用作中的数组 我的控制器名是Useraccount致命错误:无法在第38行的C:\xampp\htdocs\ov400\application\views\Useraccount\Userlist.php中将stdClass类型的对象用作数组,php,html,codeigniter,Php,Html,Codeigniter,I get error致命错误:无法将stdClass类型的对象用作中的数组 我的控制器名是Useraccount function userList() { $result['data'] = $this->Useraccount_mod->getUser_list(); $data['userlist']=$result['data'] ; $this->load->view('Useraccount/Userlist', $data); }
function userList() {
$result['data'] = $this->Useraccount_mod->getUser_list();
$data['userlist']=$result['data'] ;
$this->load->view('Useraccount/Userlist', $data);
}
模型名为Useraccount\u mod
function getUser_list() {
$query=$this->db->query("select * from users");
return $query->result();
}
视图名为Userlist
<tbody>
<?php
if ($userlist > 0) {
foreach ($userlist as $row) {
?>
<tr>
<td width="100"><?php echo $row['username']; ?></td>
<td width="100"><?php echo $row['name']; ?></td>
<td width="100"><?php echo $row['address']; ?></td>
<td width="100"><?php echo $row['creatdate']; ?></td>
<td width="100"><?php echo $row['updateddate']; ?></td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="3" align="center"><strong>No Record Found</strong></td>
</tr>
<?php
}
?>
</tbody>
未找到任何记录
遇到一个PHP错误
严重性:错误
消息:无法将stdClass类型的对象用作数组
文件名:Useraccount/Userlist.php
电话号码:38
回溯:
您正在使用result()函数获取记录,但这会将记录作为对象而不是数组。如果要获取数组,则必须使用结果\u array()
function userList() {
$data['userlist'] = $this->Useraccount_mod->getUser_list();
$this->load->view('Useraccount/Userlist', $data);
}
方法1:
函数getUser\u list(){
$query=$this->db->query(“select*from users”)->result_array();//这里我们也可以使用result(),但它给出的是对象而不是数组
返回$query;
}
未找到任何记录
方法2:
函数getUser\u list(){
$query=$this->db->query(“从用户中选择*)->result();
返回$query;
}
未找到任何记录
您的问题是什么?你试过调试什么,甚至解决这个问题吗?我的问题是如何删除致命错误:无法将stdClass类型的对象用作arraySo,你试过调试那个错误吗?感谢兄弟给出的有价值的答案,但我已经完成了
function getUser_list() {
$query = $this->db->query("select * from users")->result_array(); //here we can use result() as well but it gives object not array
return $query;
}
<tbody>
<?php
if ($userlist > 0) {
foreach ($userlist as $row) {
?>
<tr>
<td width="100"><?php echo $row['username']; ?></td>
<td width="100"><?php echo $row['name']; ?></td>
<td width="100"><?php echo $row['address']; ?></td>
<td width="100"><?php echo $row['creatdate']; ?></td>
<td width="100"><?php echo $row['updateddate']; ?></td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="3" align="center"><strong>No Record Found</strong></td>
</tr>
<?php
}
?>
</tbody>
function getUser_list() {
$query = $this->db->query("select * from users")->result();
return $query;
}
<tbody>
<?php
if ($userlist > 0) {
foreach ($userlist as $row) {
?>
<tr>
<td width="100"><?php echo $row->username; ?></td>
<td width="100"><?php echo $row->name; ?></td>
<td width="100"><?php echo $row->address; ?></td>
<td width="100"><?php echo $row->creatdate; ?></td>
<td width="100"><?php echo $row->updateddate; ?></td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="3" align="center"><strong>No Record Found</strong></td>
</tr>
<?php
}
?>
</tbody>