2014-01-08 17 views
1

请认准此查询:result_array()的ActiveRecord在笨

$this->db->select('users.user_id, user_info.name, user_info.fam ,user_type.type_name '); 
$this->db->from('user_info'); 
$this->db->join('users', 'users.user_id = user_info.user_id'); 
$this->db->join('user_type', 'users.usr_typ_id = user_type.user_type'); 

$data= $this->db->get()->result_array(); 
var_dump($data); 

echo "<hr>"; 

foreach ($data as $row); 
{ 
    echo $row->user_id ."|"; 
    echo $row->name ."|"; 
    echo $row->fam ."|"; 
    echo $row->type_name ."|".'<br>'; 
    echo "<hr>"; 
} 

的结果:

后续代码var_dump($ data)返回以下行

array(2) { [0]=> array(4) { ["user_id"]=> string(8) "92090001" ["name"]=> string(5) "parsa" ["fam"]=> string(5) "arian" ["type_name"]=> string(13) "Administrator" } [1]=> array(4) { ["user_id"]=> string(8) "92090002" ["name"]=> string(6) "pendar" ["fam"]=> string(6) "pajouh" ["type_name"]=> string(7) "Blogger" } } 

但在“的foreach“部分每行显示以下错误:

A PHP Error was encountered 
Severity: Notice 
Message: Trying to get property of non-object 
Filename: models/user_model.php 
Line Number: 47 

如果有人能帮助我,我会很感激。

+0

什么行代码是行47号 – sas

回答

2

在这里,您使用result_array()。所以它返回带有stdobject的数组的结果。所以你必须打印下面的类型。

$i=0; 
foreach ($data as $row); 
{ 
    echo $row[$i]->user_id ."|"; 
    echo $row[$i]->name ."|"; 
    echo $row[$i]->fam ."|"; 
    echo $row[$i]->type_name ."|".'<br>'; 
    echo "<hr>"; 
    $i++; 
} 

如果您仅使用result(),那么您可以使用您的代码。

foreach ($data as $row); 
{ 
    echo $row->user_id ."|"; 
    echo $row->name ."|"; 
    echo $row->fam ."|"; 
    echo $row->type_name ."|".'<br>'; 
    echo "<hr>"; 
} 
+0

感谢您的关注和答复。 – Parsaria

2

应使用的result()代替result_array()

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

结果()是返回查询结果作为对象的数组

result_array()是返回查询结果作为纯array

或者如果您继续使用result_array(),则应该更改访问类型,如下所示:

使用$row['name']而不是$row->name

文档:http://ellislab.com/codeigniter/user-guide/database/results.html

+0

感谢您的关注和指导。 – Parsaria

1

您需要访问$row->user_id和其他数据在for循环中作为这样"$row['user_id']"

+0

感谢您的关注和答复。 – Parsaria

0

这些拖车的方式进行测试的,我得到的结果:

第一个是:

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

    foreach ($data as $row) 
    { 
    echo $row->user_id ."|"; 
    echo $row->name ."|"; 
    echo $row->fam ."|"; 
    echo $row->type_name ."|".'<br>'; 
    echo "<hr>"; 
    } 

第二个:

$data= $this->db->get()->result_array(); 

foreach ($data as $row) 
{ 
    echo $row['user_id'] ."|"; 
    echo $row['name'] ."|"; 
    echo $row['fam'] ."|"; 
    echo $row['type_name'] ."|".'<br>'; 
    echo "<hr>"; 

}