一、PDO连接
<?php //数据库连接参数 $db=[ 'type'=>'mysql', 'host'=>'localhost', 'dbname'=>'pipysoft', 'username'=>'root', 'password'=>'123456', ]; //DSN $dsn="{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; try { $pdo = new PDO($dsn, $db['username'], $db['password']); print_r($pdo); }catch (PDOException $e){ die('Connection Failed: ' . $e->getMessage()); }
二、PDO添加
<?php //连接数据库 require __DIR__ . '/conn.php'; //创建SQL语句模板 $sql = 'INSERT INTO `users` SET `user`= :user, `password`=:password, `email`=:email, `youname`=:youname, `address`=:address, `content`=:content, `addtime`=:addtime'; //创建SQL语句对象 $stmt = $pdo->prepare($sql); //将变量绑定到SQL语句模板的命名占位符上 $user='zhenbo'; $password = '123456'; $email = '887995@qq.com'; $youname = '皮皮'; $address = '河北省邢台市'; $content='介绍'; $addtime=time(); $stmt->bindParam('user', $user, PDO::PARAM_STR); $stmt->bindParam('password', $password, PDO::PARAM_STR); $stmt->bindParam('email', $email, PDO::PARAM_STR); $stmt->bindParam('youname', $youname, PDO::PARAM_STR); $stmt->bindParam('address', $address, PDO::PARAM_STR); $stmt->bindParam('content', $content, PDO::PARAM_STR); $stmt->bindParam('addtime', $addtime, PDO::PARAM_INT); //执行SQL操作 if ($stmt->execute()) { if ($stmt->rowCount() > 0) { echo '成功添加' . $stmt->rowCount(). '条记录, 该记录的主键id是: ' . $pdo->lastInsertId(); } } else { die('<pre>' . print_r($stmt->errorInfo(), true)); } //销毁PDO关闭数据库连接 $pdo = null;
二、PDO修改数据
<?php //连接数据库 require __DIR__ . '/conn.php'; //创建SQL语句模板 $sql = 'UPDATE `users` SET `password`=:password, `email`=:email where `id`=:id'; //创建SQL语句对象 $stmt = $pdo->prepare($sql); //将变量绑定到SQL语句模板的命名占位符上 $id=2; $password = '234567'; $email = 'zhenbo@qq.com'; $stmt->bindParam('id', $id, PDO::PARAM_INT); $stmt->bindParam('password', $password, PDO::PARAM_STR); $stmt->bindParam('email', $email, PDO::PARAM_STR); //执行SQL操作 if ($stmt->execute()) { if ($stmt->rowCount() > 0) { echo '成功修改了' . $stmt->rowCount(). '条数据'; } } else { die('<pre>' . print_r($stmt->errorInfo(), true)); } //销毁PDO关闭数据库连接 $pdo = null;
三、PDO删除数据
<?php //连接数据库 require __DIR__ . '/conn.php'; //创建SQL语句模板 $sql = 'DELETE FROM `users` where `id`=:id'; //创建SQL语句对象 $stmt = $pdo->prepare($sql); //将变量绑定到SQL语句模板的命名占位符上 $id=2; $stmt->bindParam('id', $id, PDO::PARAM_INT); //执行SQL操作 if ($stmt->execute()) { if ($stmt->rowCount() > 0) { echo '成功删除了' . $stmt->rowCount(). '条数据'; } } else { die('<pre>' . print_r($stmt->errorInfo(), true)); } //销毁PDO关闭数据库连接 $pdo = null;
四、PDO查询数据
1、fetch()
<?php //连接数据库 require __DIR__ . '/conn.php'; //创建SQL语句模板 $sql = 'SELECT * FROM `users` where `id`=:id'; //创建SQL语句对象 $stmt = $pdo->prepare($sql); //将变量绑定到SQL语句模板的命名占位符上 $id = 1; $stmt->bindParam('id', $id, PDO::PARAM_INT); //执行SQL操作 $stmt->execute(); //遍历 while ($userlist = $stmt->fetch(PDO::FETCH_ASSOC)) { echo '<pre>' . print_r($userlist, true); } //销毁PDO关闭数据库连接 $pdo = null;
2、fetchAll()
<?php //连接数据库 require __DIR__ . '/conn.php'; //创建SQL语句模板 $sql = 'SELECT * FROM `users` where `id`>:id'; //创建SQL语句对象 $stmt = $pdo->prepare($sql); //将变量绑定到SQL语句模板的命名占位符上 $id = 1; $stmt->bindParam('id', $id, PDO::PARAM_INT); //执行SQL操作 $stmt->execute(); //遍历 $userlist = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($userlist as $u) { echo '<pre>' . print_r($u, true); } //销毁PDO关闭数据库连接 $pdo = null;
手写