[PDO]①②--绑定参数

2017-09-04  本文已影响0人  子木同

$stmt->bindParam

<?php
header('content-type:text/html;charset=utf-8');
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    $sql = "INSERT user(username,password,email) VALUES(:username,:password,:email)";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(":username", $username, PDO::PARAM_STR);
    $stmt->bindParam(":password", $password, PDO::PARAM_STR);
    $stmt->bindParam(":email", $email);
    $username = 'imooc1';
    $password = 'imooc1';
    $email = 'imooc@imooc.com';
    $stmt->execute();
    $username = 'mrking1';
    $password = "mrking1";
    $stmt->execute();//1
    echo $stmt->rowCount();
} catch (PDOException $e) {
    echo $e->getMessage();
}

?>

$stmt->bindValue

?

<?php
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    $sql = "INSERT user(username,password,email) VALUES(?,?,?)";
    $stmt = $pdo->prepare($sql);
    $username = 'imooc_king3';
    $password = 'imooc_king3';
    $stmt->bindValue(1, $username);
    $stmt->bindValue(2, $password);
    $stmt->bindValue(3, 'imooc@imooc.com');
    $stmt->execute();
    echo $stmt->rowCount()."<br/>";//1或0

    $stmt->bindValue(1, "imooc_king2");
    $stmt->bindValue(2, "imooc_king2");
    $stmt->bindValue(3, 'imooc@imooc.com');
    $stmt->execute();
    echo $stmt->rowCount();//1或0

} catch (PDOException $e) {
    echo $e->getMessage();
}

:username

<?php
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    $sql = "INSERT user(username,password,email) VALUES(:username,:password,:email)";
    $stmt = $pdo->prepare($sql);
    $username = "imooc_king4";
    $password = "imooc_king4";
    $stmt->bindValue(':username', $username);
    $stmt->bindValue(':password', $password);
    $stmt->bindValue(':email', 'king@imooc.com');
    $stmt->execute();
    echo $stmt->rowCount();

} catch (PDOException $e) {
    echo $e->getMessage();
}

Paste_Image.png Paste_Image.png Paste_Image.png

$stmt->columnCount()

$stmt->bindColumn

$stmt->fetch(PDO::FETCH_BOUND)

<?php
header('content-type:text/html;charset=utf-8');
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    $sql = "SELECT username,password,email FROM user";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    echo "结果集中的列数一共有:" . $stmt->columnCount() . "<hr/>";
    var_dump($stmt->getColumnMeta(0));
    $stmt->bindColumn(1, $username);
    $stmt->bindColumn(2, $password);
    $stmt->bindColumn(3, $email);
    while ($stmt->fetch(PDO::FETCH_BOUND)) {
        echo '用户名:' . $username . '密码:' . $password . '-邮箱:' . $email . '<hr/>';
    }

} catch (PDOException $e) {
    echo $e->getMessage();
}
?>
Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读