夺命雷公狗---PDO NO:9 使用PDO准备语句并执行语句

<?php
header(“Content-Type:text/html;charset=utf-8″);
try{
    $pdo = new PDO(“mysql:host=localhost;dbname=xsphp”,’root’,”);
    $pdo -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $pdo -> query(“set names utf8″);
    }catch(PDOException $e){
    echo “NO”.$e->getMessage();
    exit;
}
try{
    $stmt = $pdo -> prepare(“insert into stu(name,age) values(?,?)”);
    //先构造语句
    $stmt -> bindparam(1,$name); //合并语句
    $stmt -> bindparam(2,$age); //合并语句
    $name = ‘lisi'; //编辑语句
    $age = ’22′; //编辑语句
    $stmt -> execute(); //执行语句
    $name = ‘zhangsan';
    $age = ’55′;
    $stmt -> execute();
    $name = ‘xiaohei';
    $age = ’22′;
    $stmt -> execute();
}catch (PDOException $e){
    echo “NO”. $e ->getMessage();
}

 

posted @ 2015-12-09 14:41  夺命雷公狗  阅读(177)  评论(0编辑  收藏  举报