Home > php教程 > php手册 > body text

初识PHP(四)PDO对象配置于使用,初识pdo

WBOY
Release: 2016-06-13 08:42:13
Original
1187 people have browsed it

初识PHP(四)PDO对象配置于使用,初识pdo

一、PDO的概念

  PDO其实就是一个数据库的抽象层,使用PDO编程可以方便的在之后的实际运营中随时更改数据库而不用变更源代码。PDO的位置如下图所示:

phptest","root","123");

  这就完成了PDO对象的初始化,所连接的数据库是mysql数据库的phptest这个数据库,使用的用户名和密码分别是root和123

  如果把dsn信息写到配置文件中,则使用如下方式:

  $pdo = new PDO("uri:MysqlDbo.ini","root","123");  \\dsn数据写在MysqlDbo.ini文件中

  

  3.2 PDO对象的使用

  PDO的成员方法如下:

  1 ) query($sql);      //用于执行查询SQL语句。返回PDOStatement对象     

  2 ) exec($sql);          //用于执行增、删、改操作,返回影响行数;     

  3 ) setAttribute();     //设置一个"数据库连接对象"属性。         

  4 ) fetchAll();        //解析数据

 

  下面举例:  

   数据库原始数据如下:

   1 php 2 //连接数据库 3 try { 4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "20125202"); 5 } 6 catch (PDOException $e){ 7 die("数据库连接失败".$e->getMessage()); 8 } 9 //查询语句 10 $sql = 'select * from students'; 11 //执行语句、解析数据 12 echo '  id  '.'____________'.'name'.'____________'.'sex'."
"; 13 foreach ($pdo->query($sql) as $val){ 14 echo $val['id'].'____________'.$val['name'].'____________'.$val['sex']."
"; 15 } 16 ?>

 

效果如图:

1 php 2 //连接数据库 3 try { 4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "123"); 5 } 6 catch (PDOException $e){ 7 die("数据库连接失败".$e->getMessage()); 8 } 9 //插入语句 10 $sql = "insert into students values('20125203','tony','female')"; 11 //执行语句、解析数据 12 $res = $pdo->exec($sql); 13 if ($res){ 14 echo "插入成功!"; 15 } 16 //查询结果 17 $sql = 'select * from students'; 18 echo '  id  '.'____________'.'name'.'____________'.'sex'."
"; 19 foreach ($pdo->query($sql) as $val){ 20 echo $val['id'].'____________'.$val['name'].'____________'.$val['sex']."
"; 21 } 22 ?>

效果如图:

1 php 2 //连接数据库 3 try { 4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "123"); 5 } 6 catch (PDOException $e){ 7 die("数据库连接失败".$e->getMessage()); 8 } 9 //插入语句 10 //$sql = "insert into students values('20125203','tony','female')"; 11 //修改语句 12 $sql = "update students set sex='male' where id='20125203'"; 13 //执行语句、解析数据 14 $res = $pdo->exec($sql); 15 if ($res){ 16 echo "修改成功!"; 17 } 18 //查询结果 19 $sql = 'select * from students'; 20 echo '  id  '.'____________'.'name'.'____________'.'sex'."
"; 21 foreach ($pdo->query($sql) as $val){ 22 echo $val['id'].'____________'.$val['name'].'____________'.$val['sex']."
"; 23 } 24 ?> 修改数据

效果如图:

1 php 2 //连接数据库 3 try { 4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "20125202"); 5 } 6 catch (PDOException $e){ 7 die("数据库连接失败".$e->getMessage()); 8 } 9 //插入语句 10 //$sql = "insert into students values('20125203','tony','female')"; 11 //修改语句 12 //$sql = "update students set sex='male' where id='20125203'"; 13 //删除语句 14 $sql = "delete from students where id='20125203'"; 15 //执行语句、解析数据 16 $res = $pdo->exec($sql); 17 if ($res){ 18 echo "删除成功!"; 19 } 20 //查询结果 21 $sql = 'select * from students'; 22 echo '  id  '.'____________'.'name'.'____________'.'sex'."
"; 23 foreach ($pdo->query($sql) as $val){ 24 echo $val['id'].'____________'.$val['name'].'____________'.$val['sex']."
"; 25 } 26 ?> 删除数据

效果如图:

以上就是PDO的基本使用方法与增删查改等操作。

Related labels:
sdk
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template