"root", "password" => "root", "dbname" => "app",">
当前位置:Gxlcms > PHP教程 > php操作PDO处理数据实例

php操作PDO处理数据实例

时间:2021-07-01 10:21:17 帮助过:12人阅读

直接贴代码:

 "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教程有兴趣的朋友有所帮助。

人气教程排行