我如何将JSON对象插入MySQL数据库?

时间:2016-04-20 15:31:51

标签: php json

嗨我已经因为这个问题而受到了干扰,但我没有找到任何结果。

问题很简单:

如何将json对象插入mysql数据库以便以后检索它,json对象如下所示:

$CHE=mysqli_real_escape_string($conexio,$_POST['checkE']);//json object 1
$CHT=mysqli_real_escape_string($conexio,$_POST['checkT']);//json object 2
$CHF=mysqli_real_escape_string($conexio,$_POST['checkF']);//json object 3
$CHD=mysqli_real_escape_string($conexio,$_POST['checkD']);//json object 4
$CHA=mysqli_real_escape_string($conexio,$_POST['checkA']);//json object 5


//$query_it=mysqli_query($conexio," INSERT INTO menu values('".$personal_name."','".utf8_decode($CHE)."','".utf8_decode($CHT)."','".utf8_decode($CHF)."','".utf8_decode($CHD)."','".utf8_decode($CHA)."')");    

一次运行代码,我们可以看到数据进入mysql表如下:

{"menuE":{"Introducción":{},"Visualización":{}}}

但不是例如以下(也就是说,使用斜杠):

"{\"title\":\"Sachin vs shoaib akhtar and company..Unseen (BAAP BAAP HI HOTA HAI ) ....flv\",\"titleEsc\":\"Sachin vs shoaib akhtar and company..Unseen (BAAP BAAP HI HOTA HAI ) ....flv\",\"url\":\"http://www.youtube.com/watch?v=WOJlDpoL7tw\",\"pageUrl\":\"http://www.youtube.com/watch?v=WOJlDpoL7tw\",\"cannonicalUrl\":\"www.youtube.com\",\"description\":\"Sachin hitting cluless shoaib akhtar and company everywhere in ground..This is one f many innings where shoaib akhtar was brutually assaulted by sachin tendu...\",\"descriptionEsc\":\"Sachin hitting cluless shoaib akhtar and company everywhere in ground..This is one f many innings where shoaib akhtar was brutually assaulted by sachin tendu...\",\"images\":\"http://i2.ytimg.com/vi/WOJlDpoL7tw/hqdefault.jpg\",\"video\":\"yes\",\"videoIframe\":\"<iframe id=\\\"20131122120255WOJlDpoL7tw\\\" style=\\\"display: none; margin-bottom: 5px;\\\" width=\\\"499\\\" height=\\\"368\\\" src=\\\"http://www.youtube.com/embed/WOJlDpoL7tw\\\" frameborder=\\\"0\\\" allowfullscreen></iframe>\"}";

因为如果它与上面的结构一起保存,我们可以检索执行以下操作的信息:

$array=json_decode($objstring,true);
//var_dump($array);


foreach($array  as $titol=>$llista){

    echo "TITOL: ".$titol."   LLISTA:  ".$llista;;
    echo"<br>";
    }

那我怎么能保存json对象?

由于

1 个答案:

答案 0 :(得分:0)

我会尝试使用json_encode()

对它们进行字符串化
$CHE=mysqli_real_escape_string($conexio,json_encode($_POST['checkE']));//json object 1