2015-05-05 17 views
-1

我是從數據的基礎上採用面向對象的獲取數據,這讓我在陣列數據作爲如何使用oop從mysql獲取數據..?

array(23) { 
    [0]=> array(6) { 
    [0]=> string(1) "6" 
    ["id"]=> string(1) "6" 
    [1]=> string(17) "Office Management" 
    ["Name"]=> string(17) "Office Management" 
    [2]=> string(1) "3" 
    ["top_parent_id"]=> string(1) "3" 
    } 
} 

當我嘗試GE數據從陣列它提供了錯誤的

「注意:試圖獲取屬性非對象在C:\ XAMPP \ htdocs中\接力\ test.php的第12" 行

這裏是我的代碼

require_once('class.php'); 

$myClassObj = new db(); 

//$myClassObj->db(); 
$data = $myClassObj->select("parent_menu"); 
//var_dump($data); 
foreach($data as $list) 
{ 
    echo $list->{'0'}->id."<br>"; 
} 
+0

是什麼的print_r的'輸出($列表)' – Noman

回答

0

當我看到結果。 $data是包含所有數據的對象,$list是數組。你可以像

$list['id']; 
$list[0]; 

獲取使用索引或鍵列表數據都將工作

+0

它應該是'$列表[0] [ '身份證' ]'。 –

1

$dataarray,並且您試圖訪問像object值。試着用 -

echo $list[0]['id']."<br>"; 
0

$list->{'0'}->id."<br>";意味着你正在嘗試訪問對象

的對象,但你有數組的數組,以便嘗試: -

echo $list[0]['id']; 
0
array(23) { 
    [0]=> array(6) { 
    [0]=> string(1) "6" 
    ["id"]=> string(1) "6" 
    [1]=> string(17) "Office Management" 
    ["Name"]=> string(17) "Office Management" 
    [2]=> string(1) "3" 
    ["top_parent_id"]=> string(1) "3" 
    } 
    [1] => array(6) { 
    [0]=> string(1) "7" 
    ["id"]=> string(1) "7" 
    [1]=> string(17) "Office Management" 
    ["Name"]=> string(17) "Office Management" 
    [2]=> string(1) "3" 
    ["top_parent_id"]=> string(1) "3" 
    } 
} 

如果上述數據是樣品結果

$data = $myClassObj->select("parent_menu"); 

那麼你有一個多維數組

,因爲你正在使用一個foreach

foreach($data as $list) 
{ 
    // $list means you are accessing the index by index of 
    // $data like $data[0], $data[1] until to the end of the 
    // array 
    // so list is an array already with properties 
    // to access it 

    $list['id']; 
    $list['Name']; 
    $list['top_parent_id']; 

    // to add some error checking you can use 
    // isset to check if property is existing to the array 
    if (isset($list['id'])) { 

    } 
} 
相關問題