2013-10-02 35 views
1

我有我必須轉換爲JSON變量並將其存儲在MySQL表字段的陣列,並且我通過下面的代碼做到這一點:如何將json對象插入到mysql字段中並將其讀回?

$arr = array(
     'title_it' => $category->title->attributes()->it, 
     'desc_it' => $category->desc->attributes()->it, 
     'tags_it' => $category->tags->attributes()->it, 

     'title_es' => $category->title->attributes()->es, 
     'desc_es' => $category->desc->attributes()->es, 
     'tags_es' => $category->tags->attributes()->es, 

     'title_fr' => $category->title->attributes()->fr, 
     'desc_fr' => $category->desc->attributes()->fr, 
     'tags_fr' => $category->tags->attributes()->fr, 

     'title_en' => $category->title->attributes()->en, 
     'desc_en' => $category->desc->attributes()->en, 
     'tags_en' => $category->tags->attributes()->en, 

     'title_de' => $category->title->attributes()->de, 
     'desc_de' => $category->desc->attributes()->de, 
     'tags_de' => $category->tags->attributes()->de 
     ); 
    $params = mysql_real_escape_string(json_encode($arr)); 

    $query = mysql_query("INSERT INTO category_tags (id, params) VALUES ($id, '$params')") or die("could not connect"); 

然後我想讀這個字段和僅顯示屬性title_it我試過類似:

$query = mysql_query("SELECT * FROM article_tags WHERE id = $id LIMIT 0,1") or die("could not connect");  
$row = mysql_fetch_array($query); 
$jsoni = json_encode($row['params']); 

$decoded = json_decode($jsoni, true); 
echo $decoded->title_it; 

但沒有結果。另外,json以一種奇怪的格式存儲。 mysql字段如下所示:

{「title_it」:{「0」:「titolo1」},「desc_it」:{「0」:「descrizione1」},「tags_it」:{「0」:「 tags1 「},」 title_es 「:{」 0 「:」 titulo1 「},」 desc_es 「:{」 0 「:」 descripci \ u00f3n1 「},」 tags_es 「:{」 0 「:」 etiquetas1 「},」 title_fr 「:{」 0 「:」 titre1 「},」 desc_fr 「:{」 0 「:」 內容描述 「},」 tags_fr 「:{」 0 「:」 balises1 「},」 title_en 「:{」 0 「:」 TITLE1 「},」 desc_en 「:{」 0 「:」 內容描述 「},」 tags_en 「:{」 0 「:」 tags1 「},」 title_de 「:{」 0 「:」 titel1 「},」 desc_de「: {「0」:「beschreibung1」},「tags_de」:{「0」:「etikett1」}}

那麼...什麼是正確的方式來插入這個json到mysql字段,然後只讀這個字段的參數?

+0

請不要**在新的應用程序中使用mysql_query。它已被棄用,如果使用不正確會導致危險,並且正在從未來版本的PHP中刪除。像[PDO這樣的現代化替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/)。像[PHP正確的方式](http://www.phptherightway.com/)這樣的指南將幫助您避免出現這樣的錯誤。 – tadman

+0

您正在寫'category_tags'並從'article_tags'讀取? –

+0

你爲什麼要做'$ jsoni = json_encode($ row ['params']);'? 「params''已經不是json字符串了嗎?我認爲你只需要'$ decode = json_decode($ row ['params'],true);'P.S. ',true'使它成爲一個關聯數組,*不是*一個對象。你需要'echo $ decoded ['title_it'];' –

回答

0

的問題是,SimpleXML的不回你的字符串,當你得到屬性值。當您執行$category->title->attributes()->it時,您實際上正在返回一個SimpleXMLElement對象。它看起來像這樣:

object(SimpleXMLElement)#3 (1) { 
    [0]=> 
    string(3) "Your_Value" 
} 

當JSON序列化,這是轉換爲:{0: "Your_Value"},那就是當你解碼你看到的。

你需要扮演他們作爲字符串時將它們添加到您的數組:

$arr = array(
    'title_it' => (string)$category->title->attributes()->it, 
    'desc_it' => (string)$category->desc->attributes()->it, 
    'tags_it' => (string)$category->tags->attributes()->it, 
    # etc. 
); 

當你得到你的數據,你就不必再json_encode它,你只需要json_decode吧。

$query = mysql_query("SELECT * FROM category_tags WHERE id = $id LIMIT 0,1") or die("could not connect"); 
$row = mysql_fetch_array($query); 

$decoded = json_decode($row['params'], true); 
// The ",true" makes it into an array, not an object 
echo $decoded['title_it']; 
-1

以及它看起來像你的

$category->title->attributes()->it, 

類型值返回數組,而不是字符串。正如我們知道PHP數組始終聯想(嘆氣),讓您得到

Array(0=>"titolo1") 

成爲對象

{"0":"titolo1"} 
+0

這不完全正確。 PHP數組不是***總是聯想。這可能是因爲「數組」實際上是一個對象,或者是因爲它的鍵被修改了(像'array_splice'這樣的東西,所以它們沒有按順序)。 –

+0

<類別ID = 「1」> \t <標題它= 「titolo1」 ES = 「titulo1」 FR = 「titre1」 EN = 「TITLE1」 去= 「titel1」/> \t <遞減它=「descrizione1 「es =」descripci貿n1「fr =」description1「en =」description1「de =」beschreibung1「/> \t

+0

這是我用這段代碼閱讀的xml文件:foreach($ xml-> category as $ category){....} –

相關問題