Home > Backend Development > PHP Tutorial > PHP method to implement app interface and return json data

PHP method to implement app interface and return json data

墨辰丷
Release: 2023-03-27 12:32:02
Original
2062 people have browsed it

This article mainly introduces the method of php implementing the app interface and returning json data. Interested friends can refer to it. I hope it will be helpful to everyone.

Step one: conn.PHP file, used to connect database and define the interface format, the code is as follows :

<?php 
 header("charset=utf-8"); 
 $servername="localhost"; 
 $username="root"; 
 $password="root"; 
 $dbname="test"; 
 $conn = mysql_connect($servername,$username,$password); 
 if(!$conn){ 
  echo "数据库连接失败!"; 
 } 
 mysql_select_db($dbname); 
 class Response{ 
  public static function json($code,$message="",$data=array()){ 
   $result=array( 
    &#39;code&#39;=>$code, 
    &#39;message&#39;=>$message, 
    &#39;data&#39;=>$data 
   ); 
   //输出json 
   echo json_encode($result); 
   exit; 
  } 
 } 
 
?>
Copy after login

Step 2: text.php, used to convert the data in the database into a json string and output:

<?php 
 require_once(&#39;conn.php&#39;); 
 /* 
  *选择数据表 
  * */ 
 $sqla = "SELECT * from user"; 
 $result = mysql_query($sqla,$conn); 
 $dataarr = array(); 
 while($row = mysql_fetch_array($result)){ 
  $dataarr[]=$row; 
 } 
 $id=$_GET[&#39;id&#39;]; 
 if($id==1){ 
  Response::json(1,&#39;数据返回成功&#39;,$dataarr); 
 }else if($id==2){ 
  Message::json(0,&#39;失败&#39;); 
 } 
  
?>
Copy after login

The third step: text.html, ajax loads json data and displays:

<!DOCTYPE html> 
<html> 
 <head> 
  <meta charset="UTF-8"> 
  <title></title> 
  <script src="jquery/2.0.0/jquery.min.js"></script> 
 </head> 
 <body> 
  <input id="text" type="text"/> 
  <input type="button" id="tijiao" value="提交" /> 
  <p id="tex"></p> 
  <script type="text/javascript"> 
   $("#tijiao").click(function(){ 
    var data={"id":$("#text").val()} 
    $.get("text.php?flag=showmessage",data,function(res){ 
     res=JSON.parse(res);//<span style="color:#cc0000;">将json字符串转化为json对象</span> 
     if(res.code==1){ 
      $("#tex").empty(); 
      $.each(res.data, function(x,y) { 
       $("#tex").append("id:"+y.id+"/姓名:"+y.username+"<br>"); 
      }); 
     } 
    }) 
   }) 
    
  </script> 
 </body> 
</html>
Copy after login

Related recommendations:

getJSON() asynchronously requests the server to return json format data (graphic tutorial)

JSON object definition in JS Detailed explanation of the value implementation steps

Json-lib processing solution when using frameworks such as Ajax or Easyui (graphic tutorial)

The above is the detailed content of PHP method to implement app interface and return json data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template