Blogger Information
Blog 32
fans 0
comment 0
visits 27411
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP使用PDO对象操作MySQL数据库
Yang_Sir
Original
738 people have browsed it

PDO:PHP数据库连接对象,可以连接各种PHP支持的数据库
MySql:关系型数据库管理系统,存储管理数据
数据库基本操作:增删改查insert,delete,update,select

流程

1.在本地mysql数据库中创建一张表:merchant_copy,包含字段id,merchant_name,city,address, phonecreate_time
2.定义一个Db类,创建连接和操作数据库的方法
3.依次调用增删改查方法

1.创建数据表

表名:merchant_copy

字段 数据类型 长度 备注
id int 2 主键
merchant_name varchar 100 商户名称
city varchar 50 所在城市
create_time int 11 创建时间
update_time int 11 更新时间
address varchar 255 地址
phone char 12 电话

2.创建Db类

  1. <?php
  2. //数据库操作类
  3. class Db{
  4. public static $link = null;//保存pdo连接
  5. public static $sql = null;//保存语句
  6. //使用构造函数连接数据库
  7. public function __construct($dsn,$username,$password){
  8. try{
  9. self::$link = new PDO($dsn,$username,$password);
  10. if(empty(self::$link)){
  11. echo '数据库连接失败';
  12. }
  13. }catch(Exception $e){
  14. echo $e->getMessage();
  15. }
  16. }
  17. //新增数据
  18. public static function add($data){
  19. //预处理sql语句,使用占位符
  20. $sql = "INSERT `merchant_copy` SET `merchant_name`= ? , `city`=?, `address`=?, `phone`=?,`create_time`=?";
  21. $stmt = self::$link->prepare($sql);
  22. //执行查询语句
  23. $stmt->execute($data);
  24. if($stmt->rowCount()===1){
  25. return self::$link->lastInsertId();//获取新增的主键ID
  26. }else{
  27. return $stmt->errorInfo();
  28. }
  29. }
  30. //查询数据
  31. public static function find($sql){
  32. //预处理sql语句
  33. $stmt = self::$link->prepare($sql);
  34. //执行查询语句
  35. $stmt->execute();
  36. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  37. }
  38. //更新数据
  39. public static function update($data)
  40. {
  41. //预处理sql语句,使用占位符
  42. $sql = "update `merchant_copy` SET `address`=?, `phone`=?,`update_time`=? where `id`=?";
  43. $stmt = self::$link->prepare($sql);
  44. //执行查询语句
  45. $stmt->execute($data);
  46. if($stmt->rowCount()){
  47. return $stmt->rowCount();//返回受影响的记录数
  48. }else{
  49. return $stmt->errorInfo();
  50. }
  51. }
  52. //删除数据
  53. public static function delete($table,$where)
  54. {
  55. $sql = "DELETE FROM $table WHERE {$where}";
  56. $stmt = self::$link->prepare($sql);
  57. $stmt->execute();
  58. if($stmt->rowCount()){
  59. return $stmt->rowCount();//返回受影响的记录数
  60. }else{
  61. return $stmt->errorInfo();
  62. }
  63. }
  64. }

3.调用类中的方法操作数据

  1. //数据库连接参数,
  2. $dsn = "mysql:host=localhost;dbname=www.merchant.office;charset=utf8;port=3306;";
  3. $username = 'merchant';
  4. $password = 'merchant';
  5. $db = new Db($dsn,$username,$password);//实例化Db类
  6. //查询
  7. $sql = "select `merchant_name` from `merchant_copy` where `id` <2 ";
  8. print_r($db::find($sql));
  9. //输出Array
  10. // (
  11. // [0] => Array
  12. // (
  13. // [merchant_name] => 北门小吃
  14. // )
  15. // [1] => Array
  16. // (
  17. // [merchant_name] => 东门小吃
  18. // )
  19. // )
  20. //新增
  21. echo $db::add(['中门小吃','北京市','南门','10546254256',time()]);//新增主键:5
  22. //更新
  23. echo $db::update(['东门小吃街108号','10846254256',time(),2]);//更新1条
  24. //删除
  25. echo $db::delete('merchant_copy','id>4');//删除一条
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:还写了一个类, 不错
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!