PDO 对 mysql的基本操作
PDO扩展操作
<?php $dsn = 'mysql:dbname=yii2;host=localhost';
$user = 'root';
$password = '123456';
try
{
$dbh = new PDO($dsn,$user,$password,array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
}catch(PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
} //事务使用 - beginTransaction(),commit(),rollBack(),exec()
/* // 增加了 new PDO() 中的最后参数
try
{
$dbh->beginTransaction();
$sqlDel = "delete from country where code = 'PK'";
$sqlIn = "insert into country(code,name,pop) values('TT','TEST', 9999)";
$dbh->exec($sqlDel);
$dbh->exec($sqlIn);
$dbh->commit();
}catch(PDOException $e)
{
echo "<br />error:<br />";
echo "<pre>";
print_r($e->getMessage());
$dbh->rollBack();
}
*/ // 事务使用 - setAttribute(),beginTransaction(),commit(),rollBack()
/*
// 设置错误模式,一定要设置,不然不会回滚与抛出异常,也可以在 new PDO()最后一个参数加这个值
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
try{
$dbh->beginTransaction();
$sqlDel = "delete from country where code = 'GX'";
$sqlIn = "insert into country(code,name,population) values('PK','good','4444444')";
$delFlag = $dbh->exec($sqlDel);
$inFlag = $dbh->exec($sqlIn);
var_dump($delFlag);
var_dump($inFlag);
echo ' commit ';
var_dump($dbh->inTransaction()); // true
$dbh->commit();
var_dump($dbh->lastInsertId());
echo ' commit222222 ';
}catch(PDOException $e)
{
echo ' rollBack ';
$dbh->rollBack();
echo $e->getMessage();
}
$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
*/
// 删除 - exec()
/*
$sql = "delete from country where code = 'FK'";
$count = $dbh->exec($sql);
var_dump($count); // int(1) int(0)
*/
//新增 - exec()
/*
$sql = "insert into country(code,name,population) values('FK','yes',13000)";
$count = $dbh->exec($sql);
var_dump($count); // int(1)
*/ // 查询 - query()
/*
$sql = "select * from country where code ='AU'";
$res = $dbh->query($sql, PDO::FETCH_ASSOC);
if($res->rowCount() > 0)
{
foreach($res as $row)
{
echo "<pre>";
print_r($row);
}
}
Array
(
[code] => AU
[name] => Australia
[population] => 18886000
)
*/
// 查询 - fetchAll()
/*
$sql = "select * from country where code = :code";
$sth = $dbh->prepare($sql);
$sth->execute(array(":code"=>"AU"));
$res = $sth->fetchAll(PDO::FETCH_ASSOC);
// 也可以用在 $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC),设置只关联数组
print_r($res);
*/
/*
Array
(
[0] => Array
(
[code] => AU
[0] => AU
[name] => Australia
[1] => Australia
[population] => 18886000
[2] => 18886000
) )
Array
(
[0] => Array
(
[code] => AU
[name] => Australia
[population] => 18886000
) )
*/
// PDOStatement 操作
<?php // http://php.net/manual/zh/pdostatement.execute.php
$dsn = 'mysql:host=localhost;dbname=yii2';
$username = 'root';
$password = '123456';
try
{
$dbh = new PDO($dsn,$username,$password);
}catch(PDOException $e)
{
echo "failure : ";
echo $e->getMessage();
exit();
}
echo "<pre>"; /* 打印一条SQL预处理命令 - debugDumpParams
$name = 'GD';
$sql = "select * from country where name = :name";
$res = $dbh->prepare($sql);
$res->bindValue(":name", $name);
$res->execute();
$res->debugDumpParams();
$rr = $res->fetchAll(PDO::FETCH_ASSOC);
print_r($rr);
SQL: [40] select * from country where name = :name
Params: 1
Key: Name: [5] :name
paramno=-1
name=[5] ":name"
is_param=1
param_type=2
*/ /* 获取记录的列数 columnCount()
$sql = "select * from country";
$res = $dbh->prepare($sql);
$res->execute();
$rr = $res->columnCount();
print_r($rr); // 3 */
/* 返回受影响的行数 - rowCount(), prepare(),bindValue(),execute(),
$code = 'PK';
$sql = "update country set name = 'GD' where code = :code";
$res = $dbh->prepare($sql);
$res->bindValue(":code", $code);
$res->execute();
$affectCount = $res->rowCount();
print_r($affectCount); // 1
*/
/* 查询 - prepare(),bindValue(),fetchAll(),execute()
$name = 'good';
$sql = "select count(1) as total from country where name = :name";
$res = $dbh->prepare($sql);
$res->bindValue(":name",$name);
$res->execute();
$rr = $res->fetchAll(PDO::FETCH_ASSOC);
print_r($rr);
Array
(
[0] => Array
(
[total] => 2
) )
*/ /* 查询 - bindValue(),execute(),fetchAll()
$name = 'good';
$code = 'FK';
$sql = "select * from country where name = ? and code = ? limit 1";
$res = $dbh->prepare($sql);
$res->bindValue(1,$name);
$res->bindValue(2,$code);
$res->execute();
$rr = $res->fetchAll(PDO::FETCH_ASSOC);
print_r($rr);
Array
(
[0] => Array
(
[code] => FK
[name] => good
[population] => 4444444
) )
*/
/* 查询 - prepare(),bindValue(),execute(),fetchAll()
$name = "good";
$code = 'FK';
$sql = "select * from country where name = :name and code = :code limit 1";
$res = $dbh->prepare($sql);
$res->bindValue(":code", $code);
$res->bindValue(":name", $name);
$res->execute();
$rr = $res->fetchAll();
print_r($rr);
Array
(
[0] => Array
(
[code] => FK
[0] => FK
[name] => good
[1] => good
[population] => 4444444
[2] => 4444444
) )
*/
/* 查询 - prepare(),bindParam(),execute(),fetchAll()
$name = 'good';
$code = 'PK';
$sql = "select * from country where name = ? and code = ?";
$res = $dbh->prepare($sql);
$res->bindParam(1, $name);
$res->bindParam(2, $code);
$res->execute();
$rr = $res->fetchAll(PDO::FETCH_ASSOC);
print_r($rr);
Array
(
[0] => Array
(
[code] => PK
[name] => good
[population] => 4444444
) )
*/
// 查询 - prepare(),bindParam(),execute(),fetchAll()
/*
$name = 'good';
$code = 'PK';
$population = 4444444;
$sql = "select * from country where name = :name and code = :code and population = :population";
$res = $dbh->prepare($sql);
$res->bindParam(":code", $code);
$res->bindParam(":name", $name,PDO::PARAM_STR);
$res->bindParam(":population", $population);
$res->execute();
$rr = $res->fetchAll(PDO::FETCH_ASSOC);
print_r($rr);
Array
(
[0] => Array
(
[code] => PK
[name] => good
[population] => 4444444
) )
*/ // 查询 - prepare(),execute(),fetch()
/*
$sql = "select * from country limit 2";
$res = $dbh->prepare($sql);
$res->execute();
while($rs = $res->fetch(PDO::FETCH_ASSOC))
{
print_r($rs);
}
Array
(
[code] => AU
[name] => Australia
[population] => 18886000
)
Array
(
[code] => BR
[name] => Brazil
[population] => 170115000
)
*/
// 查询 - prepare(),execute(),fetchAll()
/*
$sql = "select * from country limit 1";
$res = $dbh->prepare($sql);
$res->execute();
$rr = $res->fetchAll();
print_r($rr);
Array
(
[0] => Array
(
[code] => AU
[0] => AU
[name] => Australia
[1] => Australia
[population] => 18886000
[2] => 18886000
) )
*/
// 查询 - prepare(),execute(),fetchAll()
/**
$sql = "select * from country limit 1";
$sth = $dbh->prepare($sql);
$sth->execute();
$res = $sth->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($res);
Array
(
[0] => Array
(
[code] => AU
[name] => Australia
[population] => 18886000
) )
*/
PDO 对 mysql的基本操作的更多相关文章
- php基础系列:从用户登录处理程序学习mysql扩展基本操作
用户注册和登录是网站开发最基本的功能模块之一,现在通过登录处理程序代码来学些下php对mysql的基本操作. 本身没有难点,主要是作为开发人员,应该能做到手写这些基本代码,算是自己加强记忆,同时希望能 ...
- PDO连接mysql数据库
1.PDO简介 PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接 ...
- PDO创建mysql数据库并指定utf8编码
<?php //PDO创建mysql数据库并指定utf8编码 header('Content-type:text/html; charset=utf-8'); $servername = &qu ...
- PDO连接mysql和pgsql数据库
PDO连接mysql数据库 <?php $dsn="mysql:host=localhsot;dbname=lamp87"; $user="root"; ...
- PDO链接mysql学习笔记
<?php //PDO链接mysql//dsn三种写法: //dsn01 $dsn = 'mysql:host=localhost;dbname=mysql'; //$dsn = 'mysql: ...
- PDO 查询mysql返回字段整型变为String型解决方法
PDO 查询mysql返回字段整型变为String型解决方法 使用PDO查询mysql数据库时,执行prepare,execute后,返回的字段数据全都变为字符型. 例如id在数据库中是Int的,查询 ...
- php PDO连接mysql以及字符乱码处理
<?php //mysql 的 PDO $dsn = "mysql:dbname=cqkx;host:localhost"; $username = "root&q ...
- 如何使用PDO查询Mysql来避免SQL注入风险?ThinkPHP 3.1中的SQL注入漏洞分析!
当我们使用传统的 mysql_connect .mysql_query方法来连接查询数据库时,如果过滤不严,就有SQL注入风险,导致网站被攻击,失去控制.虽然可以用mysql_real_escape_ ...
- pdo操纵mysql数据库
PDO是mysql数据库操作的一个公用类了,我们不需要进行自定类就可以直接使用pdo来操作数据库了,但是在php默认配置中pdo是未开启所以我们必须先在php.ini中开启它才可以使用,下文我会讲到. ...
随机推荐
- 读vue-0.6-filters.js源码
'abc' => 'Abc' function capitalize (value) { if (!value && value !== 0) return '' value = ...
- redis linux(centos) 安装
前言 redis 大家都使用过, 可以安装在windows下, 也可以安装在linux下, 一般还是linux下安装比较多. 这里来介绍一下redis在linux下的安装 一. 下载 https:// ...
- linux上可代替ftp的工具rz和sz
对于经常使用Linux系统的人员来说,少不了将本地的文件上传到服务器或者从服务器上下载文件到本地,rz / sz命令很方便的帮我们实现了这个功能,但是很多Linux系统初始并没有这两个命令,因此简单的 ...
- vue-cli keep-alive用法以及activated,deactivated
keep-alive用法 <keep-alive>是Vue的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM. include: 字符串或正则表达式.只有匹配的组件会被 ...
- js中的DOM操作汇总
一.DOM创建 DOM节点(Node)通常对应于一个标签,一个文本,或者一个HTML属性.DOM节点有一个nodeType属性用来表示当前元素的类型,它是一个整数: Element,元素 Attrib ...
- mybatis教程3(映射文件)
MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% ...
- HDU 1079 Calendar Game(规律博弈)
题目链接:https://cn.vjudge.net/problem/HDU-1079 题目: Adam and Eve enter this year’s ACM International Col ...
- Flask 中的蓝图(BluePrint)
蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开 怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查 ...
- Python中的基本数据类型的区别
set集合和dict字典的区别 唯一区别: set没有对应的value值 相同点: 都无索引,不可进行切片和根据索引进行的操作 两者都是不可哈希的可变类型 两者的内部元素是可哈希的不可变类型 利用哈希 ...
- IOS7如何获取设备唯一标识
WWDC 2013已经闭幕,IOS7 Beta随即发布,界面之难看无以言表...,简直就是山寨Android. 更让IOS程序猿悲催的是,设备唯一标识的MAC Address在IOS7中也失效了. I ...