1

当我使用 var_dump($row); 在我的代码上,网页上有一行显示资源(29,mysql链接持久)这意味着什么?我是 php 和 codeignite 的新手,所以如果可以的话,让它变得简单,非常感谢。

控制器.php

<?php

class Survay extends CI_Controller{

function index()
{
          $data = array(
         'question' => $this->input->post('question'),
          'answer1' => $this->input->post('answer1'),
          'answer2' => $this->input->post('answer2'),
          'answer3' => $this->input->post('answer3'),
          'answer4' => $this->input->post('answer4'),
          'answer5' => $this->input->post('answer5'),
          'answer6' => $this->input->post('answer6'),
           );


           if($query = $this->membership_model->get_records()){
           $data['records'] = $query;
           }

           $this->page();

           $this->load->view('survay_view', $data);

           }

//pagination
        function page()
           {

            $this->load->library('pagination');


            $config['base_url'] = 'http://localhost/admin/index.php/survay/';
            $config['total_rows'] = $this->db->get('save_survay')->num_rows();

            $config['per_page'] = 1;
            $config['num_links'] =10;

            $this->pagination->initialize($config);
            //print_r($this->uri->segment());die;

            $data['records'] = $this->db->get('save_survay', $config['per_page'], 
            $this->uri->segment(2,0));

            $data['pagination'] = $this->pagination->create_links();
            $this->load->view('survay_view', $data);

          }
    }

?>

视图.php

     </head>
   <body>


            <h1>Answer</h1>
            <?php if (isset($records)) : foreach($records as $row) : ?>

      <div = 'container'>




          <ul>
             <?php 
          if (isset($pagination))
            {
            echo $pagination;
            } 


           ?>
           <h1><?php  echo  $row->question; ?></h1>

           <li><?php  echo  $row->answer1; ?></li>
           <li><?php  echo  $row->answer2; ?></li>
             <li><?php  echo  $row->answer3; ?></li>
             <li><?php  echo  $row->answer4; ?></li>
             <li><?php  echo  $row->answer5; ?></li>
             <li><?php  echo  $row->answer6; ?></li>
          <ul>

       </div>

          <?php endforeach; ?>
          <?php else : ?>
          <h2>no records were returned</h2>
          <?php endif; ?>
    </body>
</html>
4

1 回答 1

0

通知消息:试图获取非对象文件名的属性:views/survay_view.php 行号:33

您收到上述错误的原因是因为您试图访问 $records 数组中作为对象保存的数据$row->some_variable,并且看起来该数据不是对象。

您正在使用以下代码从数据库中获取结果:

$data['records'] = $this->db->get('save_survay', $config['per_page'], $this->uri->segment(2,0));

上面的代码在数据库上运行一个查询,但仅此一项并没有以有用的格式生成任何结果。它返回的是一个数组,其中包含有关查询的各种数据,其中是结果。

要将结果作为对象访问,您可以按如下方式调整代码:

$data['records'] = $this->db->get('save_survay', $config['per_page'], $this->uri->segment(2,0))->result();

如果您希望将结果作为数组返回,您可以使用以下内容:

$data['records'] = $this->db->get('save_survay', $config['per_page'], $this->uri->segment(2,0))->result_array();

正如我在上面的评论中提到的,我真的建议阅读文档,尤其是关于生成查询结果的页面

于 2013-02-14T13:04:33.527 回答