코드 직접 게시:
<?php if(!defined("APP")){ exit("No direct script access allowed"); } class App{ private static $pdo = null; /** * @获取数据库配置 * @return array */ public static function getConfig(){ if($_SERVER['SERVER_NAME'] == 'www.app.com'){ return array( "host" => "127.0.0.1", "user" => "root", "password" => "root", "dbname" => "app", "apiurl"=>"http://127.0.0.1:2348/api/v1/saveUserIPInfo" ); }else{ return array( "host" => "192.168.145.190", "user" => "root", "password" => "root", "dbname" => "app", "apiurl"=>"http://192.168.145.190:2348/api/v1/saveUserIPInfo" ); } } /** * @模拟get请求 * @param $url * @return mixed|string */ public static function httpGet($url){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_TIMEOUT,1000); curl_setopt($ch,CURLOPT_HEADER,0); $res = ""; $res = curl_exec($ch); curl_close($ch); return $res; } /** * @模拟post请求 * @param $url * @param array $query * @param array $header * @return mixed */ public static function httpPost($url,$query=array(),$header=array("Content-Type" =>"application/x-www-form-urlencoded")) { $ch =curl_init(); $query = http_build_query($query); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, $query); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSLVERSION, 1); $ret = curl_exec($ch); curl_close($ch); return $ret; } /** * @ 获取数据库连接对象 * @param $config * @return null|PDO */ public static function getPdo($config){ $dsn = "mysql:host=".$config["host"].";dbname=".$config['dbname']; //echo $dsn."<br/>"; if(empty(self::$pdo)){ try{ self::$pdo = new PDO($dsn,$config["user"],$config["password"]); //echo "connect ok"; }catch(PDOException $e){ exit("connection failed,please check"); //echo $e->getMessage(); } } return self::$pdo; } /** * @查询sql语句 * @param $sql * @param $params * @param bool $fetch true则获取查询结果 * @return array */ public static function query($sql,$params,$fetch = false){ if(empty(self::$pdo)){ exit("<br/>instance pdo first"); } $res = self::$pdo->prepare($sql); foreach($params as $k => $v){ //echo $k." bind ".$v."<br/>"; $res->bindValue($k,$v); } $res->execute(); if($fetch){ $retData =array(); while($row = $res->fetch(PDO::FETCH_ASSOC)){ array_push($retData,$row); } return $retData; } } /** * @解析get请求的参数 * @return array */ public static function parseQueryInfo(){ $retData = []; parse_str($_SERVER["QUERY_STRING"],$apiParam); foreach ($apiParam as $k => $v){ $apiParam[urlencode($k)] = urlencode($v);//中文转码 } $retData["ip"] = $_SERVER["REMOTE_ADDR"]; $retData["shareInfo"] = urldecode(json_encode($apiParam));//处理中文解码 return $retData; } /** * @打印数组 * @param $arr */ public static function p($arr){ echo "<pre/>"; print_r($arr); } } ?>
사용:
define("APP","READY");
require_once "common.php";
$type = isset($_GET['type']) ? $_GET['type'] : 0;
//구성 항목 가져오기
$config = DownLoad : :getConfig();
//pdo 객체 초기화
DownLoad::getPdo($config);
//기록 쿼리
$ data = DownLoad::query("SELECT * FROM `mh_app_update` WHERE status=1 AND os_type=:type ORDER BY 코드 DESC LIMIT 1",[":type"=>$type],true);
//결과 인쇄
DownLoad::p($data);
이상에서는 관련 내용을 포함하여 PDO를 사용하여 PHP에서 데이터를 처리하는 방법에 대한 예제를 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.