PHP operation PDO processing data example

WBOY
Release: 2016-07-28 08:29:11
Original
1185 people have browsed it

Post the code directly:

<?php
if(!defined("APP")){
    exit("No direct script access allowed");
}

class App{
private static $pdo = null;

/**
 * @获取数据库配置
 * @return array
 */
public static function getConfig(){
    if($_SERVER[&#39;SERVER_NAME&#39;] == &#39;www.app.com&#39;){
        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);
}

}



?>
Copy after login

Use:

define("APP","READY");
require_once "common.php";

$type = isset($_GET['type']) ? $ _GET['type'] : 0;

//Get configuration items
$config = DownLoad::getConfig();

//Initialize pdo object
DownLoad::getPdo($config);


// Query records
$data = DownLoad::query("SELECT * FROM `mh_app_update` WHERE status=1 AND os_type=:type ORDER BY code DESC LIMIT 1",[":type"=>$type],true);

//Print results

DownLoad::p($data);

The above has introduced an example of how to use PDO to process data in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!