【问题标题】:Fatal error. Cannot use object of type stdClass as array in C:\xampp\htdocs\system\libraries\Table.php on line 256致命错误。不能在第 256 行的 C:\xampp\htdocs\system\libraries\Table.php 中使用 stdClass 类型的对象作为数组
【发布时间】:2016-05-16 17:57:00
【问题描述】:

当我尝试在同一个控制器中执行 2 个函数时出现此错误。第一个工作正常,但得到错误

'致命错误。不能使用 stdClass 类型的对象作为数组 C:\xampp\htdocs\system\libraries\Table.php 在第 256 行。

控制器

<?phpif (!defined('BASEPATH'))
exit('No direct script access allowed'); class tables extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('session');
    $this->load->library('table');
    $this->load->library('pagination');
    $this->load->database();
    //load the search model
    $this->load->model('mtables');
}

public function index() {

}

public function current_users() {

    $config = array();
    $config["base_url"] = base_url() . "tables/current_users";

    $table = 'dept_officer_view';
    $total_row = $this->mtables->record_count($table);

    $config["total_rows"] = $total_row;
    $config["per_page"] = 10;
    $config['use_page_numbers'] = TRUE;
    $config['num_links'] = 20;
    $config['cur_tag_open'] = '&nbsp;<a class="current">';
    $config['cur_tag_close'] = '</a>';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Previous';

    $this->pagination->initialize($config);
    $result_per_page = 10;
    $data["links"] = explode('&nbsp;', $str_links);

    $datatable1 = $this->mtables->fetch_data($result_per_page, $this->uri->segment(3), $table);
    $this->load->view('header');
    $this->load->view('dept_officer_table_view', array(
        'datatable1' => $datatable1,
        'result_per_page' => $result_per_page
    ));
    $this->load->view('footer');
}

public function crpo_users() {

    $config = array();
    $config["base_url"] = base_url() . "tables/crpo_users";

    $table = 'crpo_view';
    $total_row = $this->mtables->record_count($table);

    $config["total_rows"] = $total_row;
    $config["per_page"] = 10;
    $config['use_page_numbers'] = TRUE;
    $config['num_links'] = 20;
    $config['cur_tag_open'] = '&nbsp;<a class="current">';
    $config['cur_tag_close'] = '</a>';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Previous';

    $this->pagination->initialize($config);
    $result_per_page = 10;

    $datatable2 = $this->mtables->fetch_data($result_per_page, $this->uri->segment(3), $table);


    $this->load->view('header');
    $this->load->view('crpo_table_view', array(
        'datatable2' => $datatable2,
        'result_per_page' => $result_per_page));
    $this->load->view('footer');
}
) 
?>

型号

<?php
class mtables extends CI_Model {

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

// Count all record of table "contact_info" in database.
public function record_count($table) {
    return $this->db->count_all("$table");
}

// Fetch data according to per_page limit.
public function fetch_data($limit,$start,$table) {
    $this->db->limit($limit, $start*$limit);
    $query = $this->db->get("$table");
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }

        return $data;
    }
    return false;
}
}?>

查看 1 crpo_table_view.php

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<div id="wrapper">
<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header"> Users</h1>
            <ol class="breadcrumb">
                <li><a href="index.html">Dashboard</a></li>
                <li>View Users</li>
                <li class="active">Child Rights Promoting Officer</li>
            </ol>
        </div>
        <div class="col-lg-3 col-sm-offset-1">
            <a href="<?= base_url('users') ?>" class="btn btn-default">Department Officers</a>
        </div>
        <div class="col-lg-4">
            <a href="<?= base_url('crpo') ?>"class="btn btn-default active">Child Rights Promoting Officers</a>
        </div>
        <div class="col-lg-4">
            <a href="<?= base_url('donors') ?>"class="btn btn-default">Donors/ Foster Parents</a>
        </div>
    </div>
    <!-- pagination -->                
    <div>

        <?php
        // generate the table
        $this->table->set_heading('Fisrt name', 'Last name', 'Contact no','Username', 'Email','Official Address','DS Division','District');
        echo $this->table->generate($datatable2);

        // generate the page navigation

        echo $this->pagination->create_links();
        ?>

    </div>



</div>
</div>

查看 2 donor_table_view.php

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<div id="wrapper">
<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header"> Users</h1>
            <ol class="breadcrumb">
                <li><a href="index.html">Dashboard</a></li>
                <li>View Users</li>
                <li class="active">Donors/Foster Parents</li>
            </ol>
        </div>
        <div class="col-lg-3 col-sm-offset-1">
            <a href="<?= base_url('users') ?>" class="btn btn-default">Department Officers</a>
        </div>
        <div class="col-lg-4">
            <a href="<?= base_url('crpo') ?>"class="btn btn-default">Child Rights Promoting Officers</a>
        </div>
        <div class="col-lg-4">
            <a href="<?= base_url('donors') ?>"class="btn btn-default active">Donors/ Foster Parents</a>
        </div>
    </div>
    <!-- pagination -->                
    <div>

        <?php
        // generate the table
        $this->table->set_heading('Fisrt name', 'Last name', 'Contact no','Address','Username', 'Email');
        echo $this->table->generate($datatable3);

        // generate the page navigation

        echo $this->pagination->create_links();
        ?>

    </div>



</div>
</div>

【问题讨论】:

  • 哪一行代码出错了?
  • 只需将出错的文件代码放入其中,删除所有其他文件,并通过注释或标记说明出错的行
  • 在系统文件中获取错误,C:\xampp\htdocs\Codeigniter\system\libraries\Table.php 位于第 256 行 protected function _prep_args($args) { // If there is no $args[0], skip this and treat as an associative array // This can happen if there is only a single key, for example this is passed to table-&gt;generate // array(array('foo'=&gt;'bar')) if (isset($args[0]) &amp;&amp; count($args) === 1 &amp;&amp; is_array($args[0]) &amp;&amp; ! isset($args[0]['data'])) { $args = $args[0]; } foreach ($args as $key =&gt; $val) { is_array($val) OR $args[$key] = array('data' =&gt; $val); } return $args; }
  • @Kenney 中的第 196 行

标签: php mysql codeigniter pagination


【解决方案1】:

所以,问题出在:

function _prep_args($args) {
  // If there is no $args[0], skip this and treat as an associative array
  // This can happen if there is only a single key, for example this is passed to table->generate 
  // array(array('foo'=>'bar'))
  if (isset($args[0]) && count($args) === 1 && is_array($args[0]) && ! isset($args[0]['data'])) {
    $args = $args[0];
  }
  foreach ($args as $key => $val) {
    is_array($val) OR $args[$key] = array('data' => $val);
  }
  return $args;
}

如果$args 不是一个数组,而是一个stdClass,那么这将解释你得到的错误。要找出使用错误参数调用它的位置,您可以添加

if ( is_object( $args ) ) throw new Exception("Bug!");

if ( is_object( $args ) ) { echo "<pre>"; debug_print_backtrace(); echo "</pre>"; }

【讨论】:

  • 谢谢。您是否在代码中找到了调用它的位置?如果您要扩展 CI_Table,则可以覆盖 protected function _prep_args($args) 以支持 stdClass,否则可能使用 (array) 演员表就足够了。或者可能就像一个错字一样简单?
  • 我找不到错误,但在不使用表库生成表时可以正常工作@kenney
  • 啊,这是规避问题的一种方法 ;-) 好吧,如果您尝试了我的回答中的两个建议之一并且得到了堆栈跟踪,也许您可​​以将其添加到您的问题中,以便我们可以找出哪里出了问题——如果你有兴趣的话。会不会是像echo $this-&gt;table-&gt;generate($datatable3);这样的行?
  • 查询的结果必须是result_array()的格式,而不是result()@Kenney
【解决方案2】:

这样就可以了:

$sql = "SELECT * FROM users";
$stmt = $pdo->query($sql);
while( $res = $stmt->fetch(PDO::FETCH_ASSOC)){
     $res['row_name'];
};

【讨论】:

  • 请解释一下你的答案
猜你喜欢
  • 1970-01-01
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多