首页 > 后端开发 > php教程 > php pdo操作数据库 总结

php pdo操作数据库 总结

WBOY
发布: 2016-06-23 13:43:45
原创
927 人浏览过

使用预处理方式:

$dbh = new PDO('mysql:host=localhost;dbname=access_control', 'root', '');$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);$dbh->exec('set names utf8');/*添加*///$sql = "INSERT INTO `user` SET `login`=:login AND `password`=:password";$sql = "INSERT INTO `user` (`login` ,`password`)VALUES (:login, :password)";$stmt = $dbh->prepare($sql);$stmt->execute(array(':login'=>'kevin2',':password'=>''));echo $dbh->lastinsertid();/*修改*/$sql = "UPDATE `user` SET `password`=:password WHERE `user_id`=:userId";$stmt = $dbh->prepare($sql);$stmt->execute(array(':userId'=>'7', ':password'=>'4607e782c4d86fd5364d7e4508bb10d9'));echo $stmt->rowCount();/*删除*/$sql = "DELETE FROM `user` WHERE `login` LIKE 'kevin_'"; //kevin%$stmt = $dbh->prepare($sql);$stmt->execute();echo $stmt->rowCount();/*查询*/$login = 'kevin%';$sql = "SELECT * FROM `user` WHERE `login` LIKE :login";$stmt = $dbh->prepare($sql);$stmt->execute(array(':login'=>$login));while($row = $stmt->fetch(PDO::FETCH_ASSOC)){	print_r($row);}print_r( $stmt->fetchAll(PDO::FETCH_ASSOC) );
登录后复制


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板