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的基本操作的更多相关文章

  1. php基础系列:从用户登录处理程序学习mysql扩展基本操作

    用户注册和登录是网站开发最基本的功能模块之一,现在通过登录处理程序代码来学些下php对mysql的基本操作. 本身没有难点,主要是作为开发人员,应该能做到手写这些基本代码,算是自己加强记忆,同时希望能 ...

  2. PDO连接mysql数据库

    1.PDO简介 PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接 ...

  3. PDO创建mysql数据库并指定utf8编码

    <?php //PDO创建mysql数据库并指定utf8编码 header('Content-type:text/html; charset=utf-8'); $servername = &qu ...

  4. PDO连接mysql和pgsql数据库

    PDO连接mysql数据库 <?php $dsn="mysql:host=localhsot;dbname=lamp87"; $user="root"; ...

  5. PDO链接mysql学习笔记

    <?php //PDO链接mysql//dsn三种写法: //dsn01 $dsn = 'mysql:host=localhost;dbname=mysql'; //$dsn = 'mysql: ...

  6. PDO 查询mysql返回字段整型变为String型解决方法

    PDO 查询mysql返回字段整型变为String型解决方法 使用PDO查询mysql数据库时,执行prepare,execute后,返回的字段数据全都变为字符型. 例如id在数据库中是Int的,查询 ...

  7. php PDO连接mysql以及字符乱码处理

    <?php //mysql 的 PDO $dsn = "mysql:dbname=cqkx;host:localhost"; $username = "root&q ...

  8. 如何使用PDO查询Mysql来避免SQL注入风险?ThinkPHP 3.1中的SQL注入漏洞分析!

    当我们使用传统的 mysql_connect .mysql_query方法来连接查询数据库时,如果过滤不严,就有SQL注入风险,导致网站被攻击,失去控制.虽然可以用mysql_real_escape_ ...

  9. pdo操纵mysql数据库

    PDO是mysql数据库操作的一个公用类了,我们不需要进行自定类就可以直接使用pdo来操作数据库了,但是在php默认配置中pdo是未开启所以我们必须先在php.ini中开启它才可以使用,下文我会讲到. ...

随机推荐

  1. Angular学习第一步

    前端开发越来越流行了,作为后端开发人员,也开始需要学习前端了.随着前端技术的发展,前端技术也越来越复杂,复杂程度不亚于后端,各种框架各种设计模式前端都用上了,什么AngularJS,Angular, ...

  2. java 判断两个时间段是否有交集

    /* 开始时间 */ Date leftStartDate = feesPreferential.getPreferentialStartTime(); /* 结束时间 */ Date leftEnd ...

  3. array与xml转换实现(转)

    <?php function xml_encode($data, $charset = 'utf-8', $root = 'so') { $xml = '<?xml version=&qu ...

  4. Python机器学习笔记:sklearn库的学习

    网上有很多关于sklearn的学习教程,大部分都是简单的讲清楚某一方面,其实最好的教程就是官方文档. 官方文档地址:https://scikit-learn.org/stable/ (可是官方文档非常 ...

  5. C语言指针基本操作

    C语言指针基本操作 指针  指针介绍 如果说C语言最有魅力的地方在哪,那么毋庸置疑,非指针莫属了. 众所周知,C语言中每个变量都有一个内存地址,可以通过&进行访问.指针是一个变量,它的值是一个 ...

  6. ubantu 安装杀毒软件 clamav

    前言: 搜索了一番安装杀毒软件的教程, 但是多有残缺不全的, 所以整理一下,以作记录 1. 添加用户 groupadd clamav useradd -g clamav -s /bin/false - ...

  7. 简要描述 JavaScript 中定义函数的几种方式

    JavaScript 中,有三种定义函数的方式: 1.函数语句:即使用 function 关键字显式定义函数.如: function f(x){ return x+1; }  2.函数定义表达式:也称 ...

  8. [PHP] 算法-删除链表中重复的结点的PHP实现

    删除链表中重复的结点: 1.定义两个指针pre和current 2.两个指针同时往后移动,current指针如果与后一个结点值相同,就独自往前走直到没有相等的 3.pre指针next直接指向curre ...

  9. [MongoDB] MongoDB增删查改

    MongoDB的三元素,数据库.集合.文档,集合就是表,文档就是行 开启MongoDB,cd切换到MongoDB的安装目录下的bin目录里,使用命令mongod 开启,参数:--dbpath 路径,把 ...

  10. CentOS6.5安装mysql以及常见问题的解决

    前言 最近在学习Linux系统,今天在安装MySQL数据库时出现很多问题,花费了两个小时终于解决,故记录下来以供大家参考.(本人目前还在学习阶段,下面写到的是自己结合网上查到的资料以及各位前辈给出的解 ...