用php寫一個取數據的接口


目錄結構:

JS:

index.js

$(function () {
	/*
	@QUERY_ID query.xml的sql語句的id @id sql裏[]的參數
	*/
    $.post("http://localhost/cwq/index.php",{QUERY_ID:"QUERY_TABLE_ATTR",id:1},function(data){
        data=setTranscoding(data);
        console.log(data);
    })
});
function setTranscoding(data){
    data= unescape(data);//處理編碼
    data = eval('(' + data + ')');
    return data;
}

lib:

dba.php

<?php
   class phpMysql_h
   {
	   var $listmysql="";
	   var $linkmysql=0;
	   var $database_ip="127.0.0.1";
           var $database_user="root";
           var $database_password="";
	   var $database_db="phptest";
	   function link_mysql()
	   {
		   $this->linkmysql=mysqli_connect($this->database_ip,$this->database_user,$this->database_password);
		   if(!$this->linkmysql)
		   {
			   echo"登錄MySQL失敗!請聯繫系統管理員!!!";
			   exit;
			   }
	   }
	   function close_mysql()
	   {
		   mysqli_close($this->linkmysql);
	   }
	   function select_db()
	   {
		    mysqli_select_db($this->linkmysql,$this->database_db)or die('數據庫選擇錯誤!請聯繫系統管理員!!!'.mysqli_connect_error());
	   }
	   function init()
	   {
		   $this->link_mysql();
		   $this->select_db();
	   }
	   function query($str)
	   {
	   		
		   // $this->listmysql=mysqli_query($this->linkmysql,$str) or die('Query failed: '. mysql_error());
		    $this->listmysql=mysqli_query($this->linkmysql,$str) or die(json_encode(array('code' => 0 )));
	   }
	   
   }
?>

JSON.php

<?php
function change_json($json){
	$results = array();
    while ($row = mysqli_fetch_assoc($json)) {
		$results[] = $row;
	}
	return $results;
}
?>
p.php

<?php
function p ($array){
	print_r($array);
}
?>
query.php

<?php
function queryStamt($query_id){
	$doc = new DOMDocument(); 
	$doc->load('./query/query.xml'); //讀取xml文件 
	$config = $doc->getElementsByTagName("query-config"); //取得query-config標籤的對象數組 
	foreach( $config as $cg ){ 
		$query = $cg->getElementsByTagName( "query" ); //取得query的標籤的對象數組
		foreach( $query as $key=>$qy){ 
			$var=$query->item($key)->getAttribute('id');
			// p($var);
			if($var==$query_id) {
				$stmt = $qy->getElementsByTagName( "stmt" ); //取得stmt的標籤的對象數組
				$queryStmt = $stmt->item(0)->nodeValue;
				return $queryStmt;
				// echo $queryStmt;
			}		
		}
	 }
}
?>

query

query.xml

<?xml version="1.0" encoding="UTF-8"?>
<query-config>
	<query id="QUERY_TABLE_ATTR">
		<stmt>
			select * from wq_attr where id=[id];
		</stmt>
	</query>
	<query id="QUERY_TABLE_BIOG">
		<stmt>
			select * from wq_blog;
		</stmt>
	</query>
	<query id="QUERY_TABLE_CATE">
		<stmt>
			select * from wq_cate;
		</stmt>
	</query>
</query-config>

index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
   new
</body>
<script src="js/jquery-2.1.3.min.js"></script>
<script src="js/index.js"></script>
</html>

index.php

<?php
 require "./lib/dba.php";
 require "./lib/p.php";
 require "./lib/JSON.php";//json轉換函數
 require "./lib/query.php";//json轉換函數
 $msql=new phpMysql_h;
 $msql->database_db="blog";
 $msql->init();
 // echo $_GET['ID'];
 // $query_id="";
 if(!empty($_POST['QUERY_ID'])){
 	$query_id=$_POST['QUERY_ID'];
 }
 if(!empty($_GET['QUERY_ID'])){
 	$query_id=$_GET['QUERY_ID'];
 }
 // echo  $query_id;
 // die();
 // $str="select * from wq_attr";
 // p($query_id);
 $str=queryStamt($query_id);
 $countGet = count($_GET);
 $countPost = count($_POST);  
 if($countGet>1){
	foreach ($_GET as $key => $value) {  
	     $str=str_replace("[".$key."]",$value,$str);
	}  
 }
 if($countPost>1){
	foreach ($_POST as $key => $value) {  
		// P($key.",".$value);
	     $str=str_replace("[".$key."]",$value,$str);
	}  
 }
 // str_replace(find,replace,string,count)參數 描述 
 // find 必需。規定要查找的值。 
 // replace 必需。規定替換 find 中的值的值。 
 // string 必需。規定被搜索的字符串。 
 // count 可選。一個變量,對替換數進行計數。
 if(!empty($str)){
	 $msql->query($str);
	 $results=change_json($msql->listmysql);//轉換json對象數組
	 echo json_encode($results);
 }else{
 	$code=array('code' => 0 );
 	echo json_encode($code);
 	return false;
 }
 
?>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章