2012-12-27 55 views
0

我想要獲取mysql數據庫中的數據並在每種情況下輸出它們與數據庫中的數據一樣。但是當我顯示它們時,只看到"Vi tri cua nguoi dung: Array"一詞而沒有看到數據。我使用JSON測試,我得到的屏幕上的結果是:無法在mysql中回顯數據

"[{" Kinhdo ":" 106.71246126888674 "," Vido ":" 10.78865449101134 "}]" 

你能幫助我嗎?

<?php 
mysql_connect("xxxx","xxx","xxx"); 
mysql_select_db("a4602996_lv"); 
$query_insert="select Kinhdo,Vido from VietMap where id = (select max(id) from VietMap)"; 
$sql = mysql_query($query_insert); 
    if(mysql_num_rows($sql)){ 
     while($row=mysql_fetch_assoc($sql)){ 
     $json[] = $row; 
    } 
} 
//print(json_encode($json).'<br/>'); 
print 'Vi tri cua nguoi dung: '.$json['Kinhdo']; 
mysql_close(); 
?> 

回答

2

使用本:

if(mysql_num_rows($sql)) 
{ 
    echo "<pre />"; 
    while($row=mysql_fetch_assoc($sql)) 
    { 
    print_r($row); 
    $json[] = $row; 
    } 
    echo "</pre>"; 
} 

,你shold提供偏移到$ json數組,例如:

$json[0]['Kinhdo']; //for 1st record 
+0

好的,謝謝... –

0

用var_dump()或print_r的()或通過陣列

0

這意味着,返回數據是一個數組循環。使用print_r而不是

+0

我使用print_r('Vi tri cua nguoi dung:'。$ json [0]);並接收「Vi tri cua nguoi dung:Array」 –

+0

只需print_r($ json);那會給你數組 –

0

您var $json是一個多維數組。

$json[] = $row; // assining array to an array index make a array in array 

你需要循環扔$json

2

嗨請考慮以下解決方案希望這將幫助你

// If you are Expecting only one row from the Query Use the following code block 

if(mysql_num_rows($sql)){ 
    while($row=mysql_fetch_assoc($sql)){ 
    $json = $row; 
    } 
} 
//print(json_encode($json).'<br/>'); 
print 'Vi tri cua nguoi dung: '.$json['Kinhdo']; 


/**************************************************************/ 

// If you are Expecting More than one row from the Query Use the following code block 

if(mysql_num_rows($sql)){ 
    while($row=mysql_fetch_assoc($sql)){ 
    $json = $row; 
    } 
} 
//print(json_encode($json).'<br/>'); 
foreach($json as $j){ 
    print 'Vi tri cua nguoi dung: '.$j['Kinhdo']; 
}