php codeigniter echo 查询结果数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15246878/
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-25 08:53:18  来源:igfitidea点击:

codeigniter echo query result array

phpsqlarrayscodeigniter

提问by user1511579

Method inside the model:

模型内部方法:

    public function get_fichas(){

    $query = $this->db->query("SELECT * FROM fichas;");

    return $query->result();

}

Then, I'm trying to pass this data to the controller. Method on the controller:

然后,我试图将这些数据传递给控制器​​。控制器上的方法:

public function listar_fichas(){

    $data['fichas_info'] = $this->fichas_model->get_fichas();
    $this->load->view('templates/header');
    $this->load->view('fichas/listar_fichas', $data);

}

When I try to list the data in a view, I get the following error:

当我尝试在视图中列出数据时,出现以下错误:

"Fatal error: Cannot use object of type stdClass as array"

“致命错误:不能使用 stdClass 类型的对象作为数组”

Here is how I'm trying to list:

这是我试图列出的方式:

View file:

查看文件:

<?php foreach($fichas_info as $row){?>
    <table>
        <tr>
            <td><?php echo $row['cod_produto'] ;?></td>
            <td><?php echo $row['nome_produto'] ;?></td>
            <td ><?php echo $row['versao'];?></td>
        </tr>
    </table>
<?php }?>

I think I'm doing something wrong on the view. Perhaps I'm passing the data incorrectly to the view. Can someone please tell what I'm doing wrong? Thank you!

我想我在视图上做错了什么。也许我将数据错误地传递给视图。有人可以告诉我我做错了什么吗?谢谢!

回答by cryptic ツ

<td><?php echo $row['cod_produto'] ;?></td>
<td><?php echo $row['nome_produto'] ;?></td>
<td><?php echo $row['versao'];?></td>

should be:

应该:

<td><?php echo $row->cod_produto ;?></td>
<td><?php echo $row->nome_produto ;?></td>
<td ><?php echo $row->versao;?></td>

The result set is an object, so each column name is a property of the object. You were accessing them as an index of an array.

结果集是一个对象,因此每个列名都是该对象的一个​​属性。您正在访问它们作为数组的索引。

回答by RJ Spiker

You are fetching results as an object, but trying to access the data as an array. There are a couple of things you could do to resolve this:

您以对象的形式获取结果,但试图以数组的形式访问数据。您可以采取以下措施来解决此问题:

Continue to access the data in the view by changing your foreach loop to the following:

通过将 foreach 循环更改为以下内容,继续访问视图中的数据:

    <?php foreach($fichas_info as $row) { ?>
        <table>
            <tr>
                <td><?php echo $row->cod_produto; ?></td>
                <td><?php echo $row->nome_produto; ?></td>
                <td><?php echo $row->versao; ?></td>
            </tr>
        </table>
    <?php endforeach; ?>

This will access the data in your object. Another option would be to change the get_fichas() function in your model to return the result as an array. Instead of using the result() function, you could use the result_array() function. To demonstrate:

这将访问您对象中的数据。另一种选择是更改模型中的 get_fichas() 函数以将结果作为数组返回。您可以使用 result_array() 函数,而不是使用 result() 函数。展示:

    public function get_fichas(){
        $query = $this->db->query("SELECT * FROM fichas;");
        return $query->result_array();
    }

This will return an array to your controller instead of an object. You can then loop through this array in your view like you would any other array.

这将向您的控制器返回一个数组而不是一个对象。然后,您可以像处理任何其他数组一样在视图中循环遍历此数组。

Have a look at http://ellislab.com/codeigniter/user-guide/database/results.htmlfor more information on generating query results with CodeIgniter.

有关使用 CodeIgniter 生成查询结果的更多信息,请查看http://ellislab.com/codeigniter/user-guide/database/results.html

回答by Ravinder Singh

Do this :

做这个 :

<?php foreach($fichas_info as $row){?>
<table>
   <tr>
    <td><?php echo $row->cod_produto ;?></td>
     <td><?php echo $row->nome_produto ;?></td>
    <td ><?php echo $row->versao;?></td>
  </tr>
  </table>
  <?php }?>

回答by Edwin Alex

Since you are fetching results as object. So you should use like this in your view.

由于您将结果作为对象获取。所以你应该像这样使用你的观点。

<?php foreach($fichas_info as $row){?>
<table>
       <tr>
        <td><?php echo $row->cod_produto ;?></td>
         <td><?php echo $row->nome_produto ;?></td>
        <td ><?php echo $row->versao;?></td>
      </tr>
      </table>
      <?php }?>

回答by karmafunk

I think you need this:

我认为你需要这个:

<?php foreach($fichas_info as $row){?>
<table><tr>
    <td><?php echo $row->cod_produto ;?></td>
     <td><?php echo $row->nome_produto ;?></td>
    <td ><?php echo $row->versao;?></td>
</tr></table>
<?php }?>