由于 var_dump 为空,为 foreach() 提供的参数无效
Invalid argument supplied for foreach() due to var_dump is null
昨天我 var_dump($this->m_test->result_getGrades());
它给了我一个数组 [1356] 现在它返回 null。谁能帮我弄清楚为什么它是 NULL?我在 PHP 和 Codeigniter 方面还是个新手,弄清楚为什么我无法从数据库中检索任何数据时压力很大。
我假设我有这个错误的原因是因为 ($this->m_test->result_getGrades())
是 NULL
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/v_display.php
Line Number: 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; ?>
再次感谢您! :)
你的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 子句中
subject
到 subjects
加入
var 转储查询本身通常不会 return 对我来说通常不多,但这取决于你转储它的时间。
<?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; ?>
为此,您不需要一直打开和关闭 php 标签,除非您想要周围有很多 html:
<?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->result() as $row
将使您能够回显从数据库返回的信息。
同样在模型的末尾,您需要添加 return $query;
否则您的模型正在获取数据而不对其执行任何操作。
你应该试试这个:
首先,您需要检查 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();
}
昨天我 var_dump($this->m_test->result_getGrades());
它给了我一个数组 [1356] 现在它返回 null。谁能帮我弄清楚为什么它是 NULL?我在 PHP 和 Codeigniter 方面还是个新手,弄清楚为什么我无法从数据库中检索任何数据时压力很大。
我假设我有这个错误的原因是因为 ($this->m_test->result_getGrades())
是 NULL
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/v_display.php
Line Number: 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; ?>
再次感谢您! :)
你的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 子句中subject
到subjects
加入
var 转储查询本身通常不会 return 对我来说通常不多,但这取决于你转储它的时间。
<?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; ?>
为此,您不需要一直打开和关闭 php 标签,除非您想要周围有很多 html:
<?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->result() as $row
将使您能够回显从数据库返回的信息。
同样在模型的末尾,您需要添加 return $query;
否则您的模型正在获取数据而不对其执行任何操作。
你应该试试这个:
首先,您需要检查 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();
}