美文网首页PHP
php入门pdo的使用--pdo 预处理

php入门pdo的使用--pdo 预处理

作者: snakeSkin | 来源:发表于2017-03-31 09:55 被阅读27次

先创建用来连接数据库的文件:

<?php 
    try{
        $dsn = "mysql:dbname=classphp;host=127.0.0.1";
        $username = "root";
        $pwd = "";
        $pdo = new PDO($dsn,$username,$pwd);
        $pdo -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    }catch(PDOException $e){
        $e -> getMessage();
        $e -> getLine();
        $e -> getCode();
        $e -> getFile();
    }
?>

创建完成后include到使用的文件中,并通过使用pdo对象来使用预处理绑定语句:

<?php
    include("pdo.php");
    try{
        $sql = "INSERT INTO PERSON(username,pwd,email) VALUE(?,?,?)";//?是占位符
        $stmt = $pdo -> prepare($sql);
        $stmt -> bindParam(1,$username);
        $stmt -> bindParam(2,$pwd);
        $stmt -> bindParam(3,$email);
        $username = "jixue1993";
        $pwd = md5(5432);
        $email = "2394562735@qq.com";
        var_dump($stmt);
        $return = $stmt ->execute();
        
        // $pwd = md5(123456);
        // //execute 可以用这个方法直接传参,不需要使用绑定参数
        // $stmt -> execute(array("user5",$pwd,"23348923@qq.com")); 
    }
    catch(PDOException $e){
        $e -> getMessage();
        $e -> getLine();
        $e -> getCode();
        $e -> getFile();
    }
?>

也可以不实用绑定的方式,而直接传参:

<?php
    include("pdo.php");
    try{
        $sql = "INSERT INTO PERSON(username,pwd,email) VALUE(?,?,?)";//?是占位符
        $stmt = $pdo -> prepare($sql);
        $pwd = md5(123456);
        //execute 可以用这个方法直接传参,不需要使用绑定参数
        $stmt -> execute(array("user5",$pwd,"23348923@qq.com")); 
    }
    catch(PDOException $e){
        $e -> getMessage();
        $e -> getLine();
        $e -> getCode();
        $e -> getFile();
    }
?>

使用别名的方式进行预处理:

<?php
    include("pdo.php");
    try{
        $sql = "INSERT INTO PERSON(username,pwd,email) VALUE(:username,:pwd,:email)";//?是占位符
        $stmt = $pdo -> prepare($sql);
        $stmt -> bindParam(":username",$username);
        $stmt -> bindParam(":pwd",$pwd);
        $stmt -> bindParam(":email",$email);
        $username = "user1";
        $pwd = md5(123456);
        $email = "18868831752@163.com";
        $stmt -> execute();
    }
    catch(PDOException $e){
        $e -> getMessage();
        $e -> getLine();
        $e -> getCode();
        $e -> getFile();
    }
?>

使用execute中直接传输数组的方式,实现预处理:

<?php
    include("pdo.php");
    try{
        $sql = "INSERT INTO PERSON(username,pwd,email) VALUE(:username,:pwd,:email)";//?是占位符
        $stmt = $pdo -> prepare($sql);
        $pwd = md5(12345);
        $array = array("username" => "user2","pwd" => $pwd,"email" => "2394562735@qq.com");
        $stmt -> execute($array);
    }
    catch(PDOException $e){
        $e -> getMessage();
        $e -> getLine();
        $e -> getCode();
        $e -> getFile();
    }
?>

使用预处理来实现select操作,并对结果集进行处理:

<?php
    include("pdo.php");
    try{
        $sql = "SELECT * FROM PERSON WHERE id > :id";
        $stmt = $pdo -> prepare($sql);
        $stmt -> execute($_GET);
        $users = $stmt -> fetchAll(PDO::FETCH_ASSOC);
        var_dump($users);
    }
    catch(PDOException $e){
        $e -> getMessage();
        $e -> getLine();
        $e -> getCode();
        $e -> getFile();
    }
?>

相关文章

网友评论

    本文标题:php入门pdo的使用--pdo 预处理

    本文链接:https://www.haomeiwen.com/subject/zdgpottx.html