我正在从 Symfony 1.4 的数据库中获取数据。
问题是我无法使用 foreach
循环和 print_r()
访问 Success 页面中的那个数组。
错误是
Warning: Invalid argument supplied for foreach().
但是当我在执行查询后在操作页面中做同样的事情时,我能够使用 foreach()
和 print_r($result)
获取数据。
为什么我无法在成功页面中获取该数组?
我正在使用的查询和循环如下:
$birthSearchQuery = Doctrine_Query::create()->select('a.bir_le_reg,a.bir_le_dob,b.bir_le_district')
->from('BirthLegalInfo as a')
->innerJoin('a.BirthStatisticalInfo as b')
->Where('bir_le_reg_no IS NOT NULL')
->andwhere('bir_le_birth_state =' . $birthState)
->andwhere('bir_le_birth_district =' . $birthDistrict)
->andwhere('YEAR(bir_le_dob)=' .$birthFromYear);
$result = $birthSearchQuery->execute(array(), Doctrine::HYDRATE_SCALAR);
它像这样使用 print_r($result)
返回给我一个数组
Array
(
[0] => Array
(
[bir_le_reg] => Delhi
[bir_le_district] => Delhi
[bir_le_dob] => 2015
)
[1] => Array
(
[bir_le_reg] => Delhi
[bir_le_district] => Delhi
[bir_le_dob] => 2014
)
[2] => Array
(
[bir_le_reg] => Delhi
[bir_le_district] => Delhi
[bir_le_dob] => 2015
)
)
我使用的foreach循环是
foreach($result as $value){
echo $value['bir_le_district'];
echo '<br />';
echo $value['bir_le_reg'];
echo '<br />';
echo $value['bir_le_dob'];
}
最佳答案
要将变量传递给 symfony 1.x 中的模板,您应该在它前面加上 $this->
。
在你的情况下,你应该这样做:
$this->result = $birthSearchQuery->execute(array(), Doctrine::HYDRATE_SCALAR);
然后,在您的模板中,您可以调用:
foreach ($result as $value) {
没有任何问题。
关于php - 如何在 Symfony 1.4 的成功页面中获取数据数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29795794/