对于数据库的操作,最简单的办法就是使用函数操作,但是比函数更简单的数据操作方法,那只有·
类了,对于数据库的操作方法,我下面介绍三个类,分别是PDO, PDOStatement, PDOException
在使用PDO操作数据库前,你的数据库必须要具备,一般来说,如果你使用php这门语言,那么你一定安装了集成开发环境,比如PHPStudy,看如下图:
一般来说使用如下命令足够了:
mysql -uroot -p:其中root是用户名,输入这条命令后,输入密码就进入了mysql的操作界面(要在mysql的bin目录下面使用这条命令)
运行如下sql语句:
create database db_phpweb;
use db_phpweb;
create table books(id int(4) auto_increment primary key,title varchar(50) not null ,author varchar(20) not null ,press char(20) not null
);
insert into books values (1,'PHP','ert','清华大学'),(2,'web','etge','湖北民族大学'),(3,'数据库','杨fdf','野鸡族大学'),(4,'数据库2','杨dg2','野鸡族大学2'),(5,'数据库3','dghd康3','野鸡族大学3');
先介绍三种类如下所示:

PDO是类,那么如何通过PDO实例化对象(PDO实例化对象也叫做数据库连接)了:操作如下所示:
$db = new PDO('mysql:host=localhost;dbname=db_phpweb','root','901026');
// $db是PDO类的对象
$dbh = new PDO('mysql:host=localhost;dbname=db_phpweb', 'root', '901026', array(
PDO::ATTR_PERSISTENT => true
));
其中:

$pdo = new PDO('mysql:host=localhost;dbname=db_phpweb','root','901026'); //建立数据库连接
}catch(PDOException $e) //$e为PDOException类的实例化对象,getMessage()为实例化对象方法,返回数据库连接错误的原因
{echo $e->getMessage();
}
$sql1 = "insert into books values (6,'计算机网络','asd','清华大学')";
$row = $pdo->exec($sql1); //如果sql语句执行成功就返回1,否则就返回0
if($row) //sql语句执行成功,输出sql语句查询到结果
{echo 'sql语句查询到结果';
}
else
{echo 'sql语句没有查询到结果';var_dump($pdo->errorInfo());
}$sql2 = 'select * from books';
$result = $pdo->query($sql2);
var_dump($result); //返回sql语句执行的结果$sql3 = 'select * from book';
$result = $pdo->query($sql3);
var_dump($pdo->errorInfo()); //返回查询失败的错误信息db_phpweb.book' doesn't exist"
如果在连接数据库的时候出错了怎么办:
连接出错抛出异常(PDOException是对象)`
try{
$db = new PDO('mysql:host=localhost;dbname=db_phpweb','root','901026');}catch(PDOException $e) //必须是连接错误, $e为PDOException类的实例化对象,getMessage()为实例化对象方法{$e->getMessage();}
PDOStatement类的实例化对象是由PDO对象使用prepare()方法实现的,具体实现方法如下所示:
$pdo = new PDO('mysql:host=localhost;dbname=db_phpweb','root','901026'); //建立数据库连接
}catch(PDOException $e) //$e为PDOException类的实例化对象,getMessage()为实例化对象方法,返回数据库连接错误的原因
{echo $e->getMessage();
}
bindParam():

execute():

$pdo = new PDO('mysql:host=localhost;dbname=db_phpweb','root','901026'); //建立数据库连接
}catch(PDOException $e) //$e为PDOException类的实例化对象,getMessage()为实例化对象方法,返回数据库连接错误的原因
{echo $e->getMessage();
}
$sql = 'select * from books where id=?';
$stmt = $pdo->prepare($sql);
$stmt->execute([1]); //?占位,此处就是索引数组,名词占位此处就是关联数组
$result = $stmt->fetchAll(PDO::PARAM_STR);
var_dump($result);
execute的使用,
fetch()

fetchAll()

$pdo = new PDO('mysql:host=localhost;dbname=db_phpweb','root','901026'); //建立数据库连接
}catch(PDOException $e) //$e为PDOException类的实例化对象,getMessage()为实例化对象方法,返回数据库连接错误的原因
{echo $e->getMessage();
}
$sql1 = "insert into books values (7,'wer',?,?)"; //使用?占位
$sql2 = "insert into books values (8,'asdf',:title,:parse)"; //使用名字占位$stmt1 = $pdo->prepare($sql1); //sql语句预处理,后面的代码把参数带入sql语句中执行
$stmt2 = $pdo->prepare($sql2);$stmt1->bindParam(1,$one); //使用?占位,参数1是占几号位,参数二是变量名称
$stmt1->bindParam(2,$two);
$one = '肖申克的救赎';
$two = '清华大学';
$stmt1->execute();//执行sql语句
$result1 = $stmt1->fetch(); //返回一条结果
if($result1)
{var_dump($result1);$result1 = $stmt1->fetch(); //返回一条结果
}
var_dump($result1->fetch(PDO::PARAM_INT)); //一条一条返回结果
var_dump($result1->fetchAll(PDO::PARAM_INT)); //一次性返回结果$stmt2->bindParam(':title',$one); //使用名字占位
$stmt2->bindParam(':parse',$two);
$one = '肖申克的救赎1';
$two = '清华大学1';
$stmt2->execute();//执行sql语句var_dump($stmt2->fetch(PDO::PARAM_INT)); //一条一条返回结果
var_dump($stmt2->fetchAll(PDO::PARAM_INT)); //一次性返回结果
注意如上插入语句不会产生结果,如下的查询会产生结果:
$pdo = new PDO('mysql:host=localhost;dbname=db_phpweb','root','901026'); //建立数据库连接
}catch(PDOException $e) //$e为PDOException类的实例化对象,getMessage()为实例化对象方法,返回数据库连接错误的原因
{echo $e->getMessage();
}
$sql = 'select * from books where id=:id';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $id);$id=1;
$stmt->execute();
var_dump($stmt->fetchAll(PDO::PARAM_STR));
$id=2;
$stmt->execute();
var_dump($stmt->fetchAll(PDO::PARAM_STR));
//两次输出不同的值
PDO操作数
据库,除了这几个类之外,还有许多
上一篇:Mysql相关知识点