2015-06-15 40 views
0

所以我今天的問題是,如何從具有多個使用Php的JSON值的單個數據庫行獲取特定值。具有多個JSON值的數據庫行 - 如何獲得特定值

的數據已經擺在數據庫這樣:

a:7:{s:5:"cname";s:4:"enes";s:6:"cemail";s:22:"[email protected]";s:8:"caddress";s:18:"Florisvosstraat 56";s:4:"ctel";s:12:"+31685035990";s:9:"ccomments";s:5:"fdfsf";s:15:"wppizza-gateway";s:3:"cod";s:12:"wppizza_hash";s:64:"34e24d48186596cae445d52c5b0650d20c8a7c215ef1fe323ca6f09b7f804a8d";} 

所以ofcourse我已經有代碼來這是如下數據庫得到這個。

<?php 

header('content-type:application/json'); 

mysql_connect('localhost','root','') or die(mysql_error()); 

mysql_select_db('depits'); 

$select = mysql_query("SELECT * FROM `test_json`"); 

$rows=array(); 


while($row=mysql_fetch_array($select)) 
{ 
    $rows[] = $row; 
} 

echo json_encode($rows); 

?> 

我將如何能夠處理我收到的數據,並獲得例如,只有名稱和電子郵件的價值。如果這個含糊不清或者根本就不是一個好問題,請對其進行評論,以便爲了清晰起見我可能會更好。

回答

1

您的JSON似乎不正確,http://jsonlint.com/

這是我要做的事:

<? 
$test = '{"John": {"status":"Wait"},"Jennifer":{"status":"Active"},"James": {"status":"Active", "age":56,"count":10,"progress":0.0029857,"bad":0}}'; 

// decode your json into associative arrays 
$decoded = json_decode($test, true); 

//easy to read the array 
echo "<pre>"; 
print_r($decoded); 
echo "</pre>"; 

// james age 

echo "Age: ". $decoded['James']['age']; 
?> 

我想這是你在找什麼。爲了讓所有的名字,你需要一個foreach循環

<? 
$json = '{ 
"People": 
    [{ 
     "name":"peter", 
     "age" :"50" 
    },{ 
     "name":"phil", 
     "age" :"25" 
    },{ 
     "name":"mike", 
     "age" :"23" 
    }] 
}'; 

// decode your json into associative arrays 
$decoded = json_decode($json, true); 

// to loop through the array 
foreach($decoded['People'] as $key => $value) { 

    echo 'Name: ' . $value['name'] . ' Age: ' . $value['age'] . '<br>'; 

} 
?> 
+0

真的很感激 –

+0

其升技晚,但其實我是把一切都井然有序。你正在做的是單獨迴應價值,你是否有解決方案來打印好這些數字。 ($ rows = $ resultSet-> fetch_assoc()) { $ id = $ rows [/ code] {if}($ resultSet-> num_rows!= 0){ //將結果轉換爲數組 { –

+0

} 'ID']; $ customer = $ rows ['customer_ini']; $ printed = $ rows ['printed']; if($ printed == 1){ \t $ printed =「Ja」; } else { \t $ printed =「Nee」; } print json_encode($ rows); echo「

顧客詳情:$ customer
Geprint:$ printed
DeletePrint

」;「 } –

相關問題