##一、描述
Php数据库说明,采用PDO最新的数据库操作对象来进行说明。##二、使用说明
#####一、连接数据库
$dsn = 'mysql:dbname=jun_data;host=127.0.0.1'; //以;分割,第一个是数据库的名字,还有数据库名字以及地址$user = 'root'; //数据库的账号$password = 'root'; //数据库的密码try { $pdo = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage();}复制代码
#####二、增删改查
//查询User表$query = "select * from USER ";$result = $pdo->query($query);//遍历表foreach ($result as $row){ echo $row['username']; echo $row['age'];}//插入记录$insert = "INSERT INTO user VALUES(9,\"eew\",\"21\")";$result = $pdo->exec($insert);if(empty($result)){ echo "插入失败";}else if($result == 1){ echo "插入成功".$result;;}//删除记录$delete = "delete from `user` where _id=3";$result = $pdo->exec($delete);if(empty($result)){ echo "删除失败";}else if($result == 1){ echo "删除成功".$result;;}//修改记录$update = "update `user` set username=\"heh\" where _id=9";$result = $pdo->exec($update);if(empty($result)){ echo "更新失败";}else if($result == 1){ echo "更新成功".$result;;}复制代码