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. 记录前台页面一些jQuery笔记

    不知不觉工作两个月了,在这两个月,我成长很快学的很多,刚开始的时候,很多问题不懂,很多技术不会,当然作为实习阶段,很多不会的问题我都会去请教别人,在这里得感谢那些帮助我的人,但是经常去问别人问题,首先 ...

  2. Java并发编程笔记之基础总结(二)

    一.线程中断 Java 中线程中断是一种线程间协作模式,通过设置线程的中断标志并不能直接终止该线程的执行,而是需要被中断的线程根据中断状态自行处理. 1.void interrupt() 方法:中断线 ...

  3. linux下修改时间和时区

    一.修改linux的时间在root用户下,使用date指令:date -s1.只修改日期,不修改时间,输入: date -s -- 2.只修改时间,输入: date -s :: 3.同时修改日期时间, ...

  4. spring-boot-2.0.3应用篇 - shiro集成

    前言 上一篇:spring-boot-2.0.3源码篇 - 国际化,讲了如何实现国际化,实际上我工作用的模版引擎是freemaker,而不是thymeleaf,不过原理都是相通的. 接着上一篇,这一篇 ...

  5. SpringMVC源码阅读:过滤器

    1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将通过源码(基于Spring ...

  6. MySQL常用的备份方式与备份工具简介

    一.MySQL备份方式与备份类型 1.备份的必要性 再生产环境中,为了防止硬件故障.软件故障.自然灾害.误操作等各种原因导致的数据库数据丢失后能恢复到事故之前的状态,我们需要对数据库进行备份和恢复操作 ...

  7. [转]Angular 4|5 Material Dialog with Example

    本文转自:https://www.techiediaries.com/angular-material-dialogs/ In this tutorial, we're going to learn ...

  8. SQL Server 中的 NOLOCK 到底是什么意思?

    以前遇到过,但仅限于听同事说加上NOLOCK好一些,今天仔细研究测试了下,终于理解了,那么加与不加到底区别在哪呢? 我先说下其区别,之后再做测试. 大家都知道,每新建一个查询,都相当于创建一个会话,在 ...

  9. C# 基础运算符及运算

    本节主要讲述运算符的分类及其实际运用 运算符 分类 符号 解释 优先级 算数 ++  -- 加加(加1)  减减(减1) 由高到低,即执行顺序由上到下(圆括号的优先级最高) *  /  % 乘  除  ...

  10. thymeleaf-extras-db 0.0.1发布,select标签加载数据的新姿势

    在写thymeleaf页面的时候,我为了偷懒,不想为每个select下拉列表框都写一个接口,于是这个懒人jar诞生了.该jar的核心功能是直接通过thymeleaf页面的自定义标签的属性,直接运行sq ...