方法1:较简单的
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PDO方法1整理</title> </head> <body> <?php //造DSN 格式为:驱动名:dbname=数据库名;host=服务器地址 $dsn = "mysql:dbname=mydb;host=localhost"; //造PDO对象 $pdo = new PDO($dsn,"root",""); //写SQL语句 $sql = "select * from Info"; /*//执行SQL语句 可以但是一般不用 $v = $pdo->query($sql); //返回的对象比较特殊,可以直接用循环遍历 foreach($v as $a) { echo $a[0]; }*/ //预处理,先把SQL语句放到服务器上等待我们给他指令去执行 //准备一条SQL语句,返回PDO对象 $stm = $pdo->prepare($sql); //执行准备好的SQL语句,成功返回true,失败返回false if($stm->execute()) { //输出的方式 //1.逐行取数据 //var_dump($stm->fetch());//读取的数据有索引也有关联数组, //里面可以写参数 //var_dump($stm->fetch(PDO::FETCH_ASSOC));//关联数组 //var_dump($stm->fetch(PDO::FETCH_BOTH));//都有 //var_dump($stm->fetch(PDO::FETCH_NUM));//索引数组 //2.读取所有的数据 //var_dump($stm->fetchAll());//参数同上 //3.读取某一列 //var_dump($stm->fetchColumn(1));//里面是索引 //4.返回对象 //var_dump($stm->fetchObject()); } else { echo "执行失败"; } ?> </body> </html>
注意pdo中的输出方式:
逐行读取数据-----$stm->fetch(); #包含索引数组和关联数组
读取全部数据-----$stm->fetchAll(); #包含索引数组和关联数组
括号里面可以写参数的,两个的方法都是一样的,常见的有:
$stm->fetch(PDO::FETCH_ASSOC); #关联数组
$stm->fetch(PDO::FETCH_BOTH); #都有
$stm->fetch(PDO::FETCH_NUM); #索引数组
返回某一列-----$stm->fetchColumn(1); #里面是索引
返回对象-------$stm->fetchObject();
方法2:以添加数据举例,分为两种,索引数组和关联数组
索引数组
<title>PDO方法2整理</title> </head> <body> <?php //索引数组 //造对象 $dsn = "mysql:dbname=mydb;host=localhost"; $pdo = new PDO($dsn,"root",""); //写SQL语句 //$sql = "insert into Nation values(?,?)"; //预处理 $stm = $pdo->prepare($sql); //给参数绑定值,较为麻烦不建议 /*$stm->bindParam(1,$code); $stm->bindParam(2,$name); $code = "p002"; $name = "回k";*/ //造数组 $attr = array("p111","张三"); //执行 if($stm->execute($attr)) { echo "添加成功"; } else { echo "添加失败"; } ?> </body> </html>
关联数组
<title>PDO方法2整理</title> </head> <body> <?php //关联数组 //造对象 $dsn = "mysql:dbname=mydb;host=localhost"; $pdo = new PDO($dsn,"root",""); //写SQL语句 $sql = "insert into Nation values(:code,:name)"; //预处理 $stm = $pdo->prepare($sql); //给参数绑定值,麻烦不建议 /*$stm->bindParam("code",$code,PDO::PARAM_STR); $stm->bindParam("name",$name,PDO::PARAM_STR); $code = "p003"; $name = "aaaa"; */ //造数组 $attr = array("code"=>"p005","name"=>"bbbb"); //执行 if($stm->execute($attr)) { echo "添加成功"; } else { echo "添加失败"; } ?> </body> </html>
另一种方法是利用表单的,更加方便,都可以使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <form action="pdo2.php" method="post"> <div>代号:<input type="text" name="code" /></div><br /> <div>名称:<input type="text" name="name" /></div><br /> <div><input type="submit" value="提交" /></div> </form> </body> </html>
<title>PDO方法2整理</title> </head> <body> <?php //关联数组 //造对象 $dsn = "mysql:dbname=mydb;host=localhost"; $pdo = new PDO($dsn,"root",""); //写SQL语句 $sql = "insert into Nation values(:code,:name)"; //预处理 $stm = $pdo->prepare($sql); //执行 if($stm->execute($_POST)) { echo "添加成功"; } else { echo "添加失败"; } ?> </body> </html>