PHP操作mysql(mysqli + PDO)
【Mysqli面向对象方式操作数据库】
添加、修改、删除数据
$mysqli = new mysqli('localhost','root','','test');
$mysqli->query('set names utf8');
//添加数据
$result = $mysqli->query("INSERT INTO users(name,money) VALUE ('张三',10)");
$result = $mysqli->query("INSERT INTO users(name,money) VALUE ('李四',200)"); //修改数据
$result = $mysqli->query("UPDATE users SET money=money+10 WHERE id = 3"); //删除数据
$result = $mysqli->query("DELETE FROM users WHERE id=3"); var_dump($result);
查询数据
header("content-type:text/html;charset=utf-8");
$mysqli = new mysqli('localhost','root','','test');
$mysqli->query('set names utf8'); $result = $mysqli->query("SELECT * FROM users");
$data = $result->fetch_all(MYSQLI_ASSOC);
var_dump($data);
事务控制
header('content-type:text/html;charset=utf-8');
$mysqli = new mysqli('localhost','root','','test');
$mysqli->query('set names utf8');
$mysqli->autocommit(false); //开启事务
$sql1 = "UPDATE users SET money=money-10 where id=1";
$sql2 = "UPDATE users SET money=money+10 where id=20";
$mysqli->query($sql1);
$r1 = $mysqli->affected_rows;
$mysqli->query($sql2);
$r2 = $mysqli->affected_rows;
if($r1>0 && $r2>0){
$mysqli->commit(); //事务提交
echo '操作成功';
}else{
$mysqli->rollback(); //事务回滚
echo '操作失败';
}
预处理-增删改操作
header('content-type:text/html;charset=utf-8');
$mysqli = new mysqli('localhost','root','','test');
$mysqli->query('set names utf8');
$sql = "INSERT INTO users(name,money) VALUE(?,?)";
$stmt = $mysqli->prepare($sql); $name = "王小小";
$money = 500;
$stmt->bind_param('si',$name,$money);
$result = $stmt->execute();
var_dump($result); $name = "王大大";
$money = 600;
$stmt->bind_param('si',$name,$money);
$result = $stmt->execute();
var_dump($result);
预处理-查询操作
header('content-type:text/html;charset=utf-8');
$mysqli = new mysqli('localhost','root','','test');
$mysqli->query('set names utf8');
$sql = "SELECT * FROM users WHERE id>?";
$stmt = $mysqli->prepare($sql);
$id=1;
$stmt->bind_param('i',$id);
$stmt->bind_result($id,$name,$money);
$stmt->execute(); while($stmt->fetch()){
$data[] = [
'id'=>$id,
'name'=>$name,
'money'=>$money
];
}
var_dump($data);
【PDO方式操作数据库】
PDO查询数据
header('content-type:text/html;charset=utf-8');
$dsn = "mysql:host=localhost;dbname=test";
$pdo = new PDO($dsn,'root','');
$pdo->exec('set names utf8');
$sql = "SELECT * FROM users";
$stmt = $pdo->query($sql);
//$data = $stmt->fetch(PDO::FETCH_ASSOC);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($data);
PDO增删改数据
header('content-type:text/html;charset=utf-8');
$dsn = "mysql:host=localhost;dbname=test";
$pdo = new PDO($dsn,'root','');
$pdo->exec('set names utf8');
$sql = "UPDATE users SET money=500 WHERE id=1";
$result = $pdo->exec($sql);
var_dump($result);
PDO事务控制
header('content-type:text/html;charset=utf-8');
$dsn = "mysql:host=localhost;dbname=test";
$pdo = new PDO($dsn,'root','');
$pdo->exec('set names utf8');
$pdo->beginTransaction(); //开启事务
$sql1 = "UPDATE users SET money=money-100 WHERE id=1";
$r1 = $pdo->exec($sql1); $sql2 = "UPDATE1 users SET money=money+100 WHERE id=2";
$r2 = $pdo->exec($sql2); if($r1>0 && $r2>0){
$pdo->commit(); //事务提交
echo "操作成功";
}else{
$pdo->rollBack(); //事务回滚
echo "操作失败";
}
$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
//var_dump($result);
PDO预处理
header('content-type:text/html;charset=utf-8');
$dsn = "mysql:host=localhost;dbname=test";
$pdo = new PDO($dsn,'root','');
$pdo->exec('set names utf8'); //$sql = "SELECT * FROM users WHERE id>:id";
$sql = "UPDATE users SET money=1000 WHERE id=:id";
$stmt = $pdo->prepare($sql); $id = 4;
$stmt->bindParam(":id",$id); //$stmt->bindValue(1,2); $result = $stmt->execute();
var_dump($result); //$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
//var_dump($data);
常见的SQL注入方式及防范措施
header('content-type:text/html;charset=utf-8');
$dsn = "mysql:host=localhost;dbname=test";
$pdo = new PDO($dsn,'root','');
$pdo->exec('set names utf8');
$id = isset($_GET['id'])?$_GET['id']:1;
$sql = "SELECT * FROM users WHERE id=".$id;
$stmt = $pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($data); //SELECT * FROM users WHERE id=1
//SELECT * FROM users WHERE id=1 or 1=1 SELECT * FROM users
//SELECT * FROM users WHERE id=1;drop table test;--
或
header('content-type:text/html;charset=utf-8');
$dsn = "mysql:host=localhost;dbname=test";
$pdo = new PDO($dsn,'root','');
$pdo->exec('set names utf8'); $sql = "SELECT * FROM users WHERE id=:id";
$stmt = $pdo->prepare($sql);
$id = isset($_GET['id'])?$_GET['id']:1;
$stmt->bindParam(":id",$id); $result = $stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($data);
PHP操作mysql(mysqli + PDO)的更多相关文章
- MYSQL MYSQLI PDO
PHP的MySQL扩展(优缺点) 设计开发允许PHP应用与MySQL数据库交互的早期扩展.mysql扩展提供了一个面向过程 的接口: 并且是针对MySQL4.1.3或更早版本设计的.因此,这个扩展虽然 ...
- php连接MySQL数据库的三种方式(mysql/mysqli/pdo)
引言 PHP与MySQL的连接有三种API接口,分别是:PHP的MySQL扩展 .PHP的mysqli扩展 .PHP数据对象(PDO) ,下面针对以上三种连接方式做下总结,以备在不同场景下选出最优方案 ...
- php的mysql\mysqli\PDO(三)PDO
原文链接:http://www.orlion.ga/1153/ PDO是一种数据库抽象层,不止可以访问mysql还可以访问其他数据库. 一.__construct() PDO::__construct ...
- PHP 数据库连接 (Mysql Mysqli PDO)
1.PHP与Mysql扩展(本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除),PHP原生的方式去连接数据库,是面向过程的 <?php $mysql_conf = array( 'hos ...
- php的mysql\mysqli\PDO(二)mysqli
原文链接:http://www.orlion.ga/1147/ mysqli有面向对象风格和面向过程风格,个人感觉还是用面向对象风格比较好(毕竟是面向对象) 1.mysqli::_construct( ...
- php的mysql\mysqli\PDO(一)mysql
原文链接:http://www.orlion.ga/1140/ 工作中数据库的操作都被封装好了,这些怎么用的都快忘了干脆写篇博客重新复习下,以后要是再忘记了可以看这篇文章. PHP 5.5.0 起已废 ...
- MySQL原生API、MySQLi面向过程、MySQLi面向对象、PDO操作MySQL
[转载]http://www.cnblogs.com/52fhy/p/5352304.html 本文将举详细例子向大家展示PHP是如何使用MySQL原生API.MySQLi面向过程.MySQLi面向对 ...
- SQL注入实验,PHP连接数据库,Mysql查看binlog,PreparedStatement,mysqli, PDO
看到有人说了判断能否sql注入的方法: 简单的在参数后边加一个单引号,就可以快速判断是否可以进行SQL注入,这个百试百灵,如果有漏洞的话,一般会报错. 下面内容参考了这两篇文章 http://blog ...
- mysqli扩展库操作mysql数据库
配置环境 配置php.ini文件让php支持mysqli扩展库 extension=php_mysqli.dll 建库建表 详见博客 “mysql扩展库操作mysql数据库” 查询数据库 <?p ...
随机推荐
- 深入SpringMVC注解
原文链接:https://blog.csdn.net/chenpeng19910926/article/details/70837756 @Controller 在SpringMVC 中提供了一个非常 ...
- java容器(一) Collection类框架图解
- 最好用的web端代码文本编辑器ACE
使用足够简单,功能足够强大,体验足够优秀 之前有一个系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,总共发了四篇文章介绍了三个非常棒的插件,分别是bootstrap-duallistbox ...
- python day01练习和作业
习题:1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型编译型语言:优点:执行速度快 缺点:维护成本高,跨平台性差解释型语言:优点:维护成本低,跨平台性好 缺点:执 ...
- Go语言标准库之net/http
Go语言内置的net/http包十分的优秀,提供了HTTP客户端和服务端的实现. net/http介绍 Go语言内置的net/http包提供了HTTP客户端和服务端的实现. HTTP协议 超文本传输协 ...
- Codeforces_723_B
http://codeforces.com/problemset/problem/723/B 求括号内单词数和括号外最大单词长度,注意细心,尤其是ok和sum的置0. #include<iost ...
- 计蒜客A1998 Ka Chang (分块+dfs序+树状数组)
题意 给你一个\(1e5\)的有点权的树,有\(1e5\)个操作: 1.给第\(x\)层的点加上\(y\) 2.求以\(x\)为根的子树的点权和 思路 首先处理出层数为x的所有点 操作2一般都是用df ...
- Oracle 12C In-Memory特性研究
Oracle 12C In-Memory特性研究一.Oracle In-Memory1.1 In-Memory 开启方法1.2 开启与关闭IM column store1.3 inmemory优先级调 ...
- javascript js获取html元素各种距离方法
//滚动条 scrollLeft//滚动条距左边距离 scrollTop//滚动条距顶部距离 scrollWidth//滚动条元素的宽 scrollHeight//滚动条元素的高 //可视范围 cli ...
- [Redis-CentOS7]Redis集合操作(四)
SADD 集合添加 127.0.0.1:6379> SADD bbs discuz.net (integer) 1 127.0.0.1:6379> SADD bbs "tiany ...