致命错误:不能在第 38 行的 C:\xampp\htdocs\ov400\applicatio

我收到错误致命错误:不能使用 stdClass 类型的对象作为我的控制器名称中的数组是 Useraccount


function userList() {   

   $result['data'] = $this->Useraccount_mod->getUser_list();

   $data['userlist']=$result['data'] ;

   $this->load->view('Useraccount/Userlist', $data);

}

模型名称是 Useraccount_mod


function getUser_list() {

    $query=$this->db->query("select * from users");

    return $query->result();

}

和视图名称是用户列表


<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 回溯:


三国纷争
浏览 95回答 1
1回答

FFIVE

您正在使用result()函数来获取记录,但这会将记录作为对象而不是数组。如果要获取数组,则必须使用result_array()。function userList() {&nbsp; &nbsp;&nbsp; &nbsp;$data['userlist'] = $this->Useraccount_mod->getUser_list();&nbsp; &nbsp;$this->load->view('Useraccount/Userlist', $data);}方法一:function getUser_list() {&nbsp; $query = $this->db->query("select * from users")->result_array(); //here we can use result() as well but it gives object not array&nbsp; return $query;}<tbody>&nbsp; <?php&nbsp; if ($userlist > 0) {&nbsp; &nbsp; &nbsp; foreach ($userlist as $row) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row['username']; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row['name']; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row['address']; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row['creatdate']; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row['updateddate']; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; }&nbsp; } else {&nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td colspan="3" align="center"><strong>No Record Found</strong></td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <?php&nbsp; }&nbsp; ?></tbody>方法二:function getUser_list() {&nbsp; $query = $this->db->query("select * from users")->result();&nbsp;&nbsp; return $query;}<tbody>&nbsp; <?php&nbsp; if ($userlist > 0) {&nbsp; &nbsp; &nbsp; foreach ($userlist as $row) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row->username; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row->name; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row->address; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row->creatdate; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="100"><?php echo $row->updateddate; ?></td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; }&nbsp; } else {&nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td colspan="3" align="center"><strong>No Record Found</strong></td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <?php&nbsp; }&nbsp; ?></tbody>
打开App,查看更多内容
随时随地看视频慕课网APP