JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript.
Storing JSON data in PHP into a database is often encountered in project development, and I'll write an example of storing JSON in a database.
In the PHP operation database, the JSON data is stored in the database and the data is parsed from the database. Honestly, I don't know what it does, but I think it's technically something that's always useful.
The benevolent see of the beholder. PHP so far has been learning for 1.5 weeks, the shortcomings of the people, to correct, very grateful.
or build the library first: Build the library under MySQL, the code is as follows:
CREATE TABLE ' T_json ' (' id ' int (null auto_increment,' json ' varchar (NULL defaultNULL ,KEY (' id ')) ENGINE=InnoDBDefault CHARACTER Set=utf8 collate=utf8_general_ciauto_increment=18row_format=COMPACT ;
There are two fields, one is ID, and the other is JSON.
First, suppose to build a page, named input.php, the specific code is as follows:
1<form action= "json.php" method= "POST" >2<table>3<tr>4<td>id:</td>5<td>6<input type= "text" name= "userid" >7</td>8</tr>9<tr>Ten<td>class:</td> One<td> A<input type= "text" name= "UserClass" > -</td> -</tr> the<tr> -<td></td> -<td> -<input type= "Submit" value= "Submission" > +</td> -</tr> +</table> A</form>
Action to the Json.php interface, method is the Post method.
In the json.php interface, the code is as follows:
1<?PHP2@$id=$_post[' UserID '];3@$class=$_post[' UserClass '];4 $array=Array(5' UserID ' =$id,6' UserClass ' =$class,7 );8 $jsonId= Json_encode ($array);//convert array arrays into JSON data9 //$jsonId = addslashes ($jsonId);Ten One Echo $jsonId." <br> "; A Print_r($array); - - $dbms= "MySQL";//Select database type, MySQL the $host= "127.0.0.1";//Select Server - $userName= "";//User name - $PSW= ""; - $dbName= "Dbtext";//Database name + $dsn= "$dbms: host=$host;d bname=$dbName"; - + Try { A $pdo=NewPDO ($dsn,$userName,$PSW); at $query= "INSERT into T_json (JSON) VALUES (: Jsonid)";//sql statements - $request=$pdo->prepare ($query); - $request->bindparam (': Jsonid ',$jsonId); - $res=$request-execute ();//Execute SQL statement - Print_r($res); - in if(!Empty($res)){ - Echo"JSON data added successfully!!!!" "; to}Else{ + Echo"JSON Data add failed!!!!" "; - } the *}Catch(Exception $e) { $ die("error!");Panax Notoginseng } -?>
It's so simple.
Second, the database in the JSON code to parse out, this I was doing when I encountered a lot of trouble, in fact, after checking the data, found that the problem is easy to solve. The problem is that JSON parsing is not out, the solution is to traverse a bit on it.
The specific code is as follows:
1<?PHP2 $dbms= "MySQL";//Select database type, MySQL3 $host= "127.0.0.1";//Select Server4 $userName= "";//User name5 $PSW= "";6 $dbName= "Dbtext";//Database name7 $dsn= "$dbms: host=$host;d bname=$dbName";8 $arrjson=Array();9 Try {Ten $pdo=NewPDO ($dsn,$userName,$PSW); One $query= "SELECT * FROM T_json"; A $request=$pdo->query ($query); - $request-execute (); - $res=$request->fetchall (PDO::FETCH_ASSOC); the for($i= 0;$i<Count($res);$i+ +) {?> -<tr> -<td><?phpEcho $res[$i[' ID ']?></td> -<td><?php$obj =json_decode ($res [$i] [' json ']); echo "encode". $obj->userid;echo "level". $obj->userclass?></td> +</tr> -<?php} + A}Catch(Exception $e) { at die("Error".)$e-getMessage ()); - } -?>
The 18th line in the code above is parsing JSON.
JSON support is built into the version since php5.2, and there are two main functions:
Json_encode (): Encode, generate a JSON string
Json_decode (): A decoding
You can go to the following link to download the source code:
Http://pan.baidu.com/s/1bnwHjzT
PHP JSON insert and parse operations in the database