<?php 造PDO对象的参数dsn:驱动名:dbname=数据库名;host=localhost(服务器地址) $dsn="mysql:dbname=mydb;host=localhost"; 造PDO对象 $pdo=new pdo($dsn,"root","密码"); 写sql语句 $sql="select *from info"; 预处理:先把sql语句放到服务器上等待我们给他指令去执行 $shuzu=$pdo->prepare($sql); 判断执行准备好的sql语句,成功返回true,失败返回false if($shuzu->execute()) { 执行成功有四种方法 1逐行取出数据 var_dump($stm->fetch()); 2取出所有的数据 var_dump($stm->fetchAll(PDO::FETCH_OBJ)); 3取出一列 var_dump($stm->fetchColumn(1)); 4 返回对象 var_dump($stm->fetchObject()); } else { echo"执行有误"; } ?> 增加更简便的方法 <?php $dsn = "mysql:dbname=mydb;host=localhost"; $pdo = new PDO($dsn,"root","123"); SQL语句参数用?来代替 $sql = "insert into Nation values(?,?)"; $sql = "insert into Nation values(:code,:name)"; $stm = $pdo->prepare($sql); 造数组 $attr = array("p101","很快"); $attr = array("code"=>"p103","name"=>"shj"); 执行 if($stm->execute($_POST)) { echo "添加成功"; } else { echo "添加失败"; } ?>