直接贴代码:
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.
; 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(
instance pdo first); } $res = self::$pdo->prepare($sql); foreach($params as $k => $v){ //echo $k. bind .$v.
; $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
; 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 code desc limit 1,[:type=>$type],true);
//打印结果
download::p($data);
以上就介绍了 php操作pdo处理数据实例,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
