1.连接pdo,写好dsn,用户名,密码,new PDO()就ok了。
$username = 'root';
$password = 'root';
$dsn = 'mysql:host=localhost;dbname=lara5.2';
try {
$pdo = new PDO($dsn, $username, $password);
} catch(PDOException $e) {
$e->getMessage();
}
2.pdo的exec()函数,它不能用于查询语句,会返回对于数据库影响的行数,可以增、删、改。
// 增加
$sql = "insert into students (name,sno,class_id) values ('xky', '1', '1402')";
// 删除
//$sql = 'delete from students where sno = 1';
// 修改
//$sql = "update students set name ='xxx' where sno = 1";
if ($id = $pdo->exec($sql))
{
echo $id;
}
3.pdo的错误显示,errorInfo()函数,显示错误码,状态码,错误信息。
// student表不存在
$sql = 'select * from studnent';
if (!$pdo->exec($sql)) {
echo $pdo->errorCode();
// 0->错误码 1->状态 2->错误信息
var_dump($pdo->errorInfo());
}