【问题标题】:Invalid argument supplied for foreach() due to var_dump is null由于 var_dump 为 foreach() 提供的参数无效为 null
【发布时间】:2015-01-16 09:09:50
【问题描述】:

昨天我var_dump($this->m_test->result_getGrades());它给了我一个数组[1356],现在它返回了null。谁能帮我弄清楚为什么它是NULL?我还是 PHP 和 Codeigniter 的新手,弄清楚为什么我不能从我的数据库中检索任何数据是非常有压力的。

我假设出现此错误的原因是因为 ($this->m_test->result_getGrades()) 为 NULL

遇到了 PHP 错误

严重性:警告

消息:为 foreach() 提供的参数无效

文件名:views/v_display.php

行号:7

另外,我无法从数据库中检索任何数据的原因是什么?供日后参考

这是我的代码:

控制器 c_test.php

function getGrades() {
       $data['query'] = $this->m_test->result_getGrades(); 
       $this->load->view('v_display', $data);
    }

模型 m_test.php

function result_getGrades()
    {
          $this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
          $this->db->from('grades');
          $this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
          $this->db->join('subjects','subjectblocking.subjectcode=subject.subjectcode');
          $this->db->where('2013-F019');
          $query=$this->db->get();          
    }

查看 v_display.php

<?php foreach ($query as $row): ?>

               <?php echo $row->studentid;?><br>
               <?php echo $row->subjectcode;?><br>
               <?php echo $row->description;?><br>
               <?php echo $row->final;?><br>


         <?php endforeach; ?>

再次感谢您! :)

【问题讨论】:

  • 我不知道 CodeIgniter,但也许你得到 NULL 是因为查询本身,可能是 where 条件或连接导致了这个问题。

标签: php codeigniter null var-dump


【解决方案1】:

你应该试试这个:

首先,您需要检查您的 where 子句。

参见下面的示例:

$this->db->where('name', 'test');

这将在 MySQL 查询中生成 WHERE name = 'test'

那么你必须在你的 get() 方法后面加上一个 return 语句:

$query = $this->db->get();
return $query->result_array();

所以你的模型应该是这样的:

function result_getGrades()
{
      $this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
      $this->db->from('grades');
      $this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
      $this->db->join('subjects','subjectblocking.subjectcode=subject.subjectcode');

      $this->db->where('name of field', '2013-F019');

      $query=$this->db->get();  

      return $query->result_array();        
}

【讨论】:

  • 我删除了 where 子句并添加了 return $query-&gt;result_array(); 但是发生了错误,例如`在非对象上调用成员函数 result_array()`
【解决方案2】:

你的result_getGrades()函数有很多错误:

function result_getGrades()
{
    $this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
    $this->db->from('grades');
    $this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
    $this->db->join('subjects','subjectblocking.subjectcode = subjects.subjectcode');
    $this->db->where('your_field', '2013-F019');

    $query = $this->db->get()->result();

    return $query;        
}

有:

  • 添加return $query;

  • field name 在 where 子句中

  • subjectsubjects 在您的加入中

【讨论】:

  • 它仍然给我一个错误Fatal error: Call to a member function result() on a non-object
  • 我觉得这个$this-&gt;db-&gt;where('2013-F019');是假的,where子句的语法是$this-&gt;db-&gt;where('yourcolum', 'yourvalue');
  • 好的,我尝试在没有 where 子句的情况下查询它仍然是一样的
  • $this-&gt;db-&gt;join('subjects','subjectblocking.subjectcode=subject.subjectcode');这一行,将subject替换为subjects
【解决方案3】:

var 转储查询本身通常不会给我带来太多回报,但这取决于您何时转储它。

<?php foreach ($query as $row): ?>

           <?php echo $row->studentid;?><br>
           <?php echo $row->subjectcode;?><br>
           <?php echo $row->description;?><br>
           <?php echo $row->final;?><br>


     <?php endforeach; ?>

为此,除非您想要大量的 html,否则您不需要继续打开和关闭 php 标签:

<?php foreach ($query as $row){

           echo $row->studentid.'<br>';
           echo $row->subjectcode.'<br>';
           echo $row->description.'<br>';
           echo $row->final.'<br>';


     }?>

注意 . ?它将 $row->studentid 连接到 html 意味着它的代码更简洁。

现在问题是,您在 foreach 中将变量分配为变量...这是没有意义的。谢天谢地,我知道解决办法。您需要将查询转换为数据库中的结果:

<?php foreach ($query->result() as $row){

           echo $row->studentid.'<br>';
           echo $row->subjectcode.'<br>';
           echo $row->description.'<br>';
           echo $row->final.'<br>';


     }?>

$query-&gt;result() as $row 将使您能够回显从数据库中返回的信息。

同样在模型的末尾,您需要添加 return $query; 否则您的模型正在获取数据并且不对其进行任何操作。

【讨论】:

  • 添加了return $query-&gt;result_array();,但是发生了错误,例如`在非对象上调用成员函数result_array()`
  • 您在使用的每个模型/控制器上的构造中都有$this-&gt;load-&gt;database 吗?
猜你喜欢
  • 2018-05-18
  • 2011-05-17
  • 2011-02-07
相关资源
最近更新 更多