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中开启它才可以使用,下文我会讲到. ...
随机推荐
- java远程调试(idea)
遇见一个怪异问题,无奈线上数据库有限制,只能远程调试下代码.突然发现,远程调试代码真的好简单,简单记录下操作步骤. 1.在idea里创建一个Remote,远程连接的入口. 找到 Edit Config ...
- [TensorFlow] Creating Custom Estimators in TensorFlow
Welcome to Part 3 of a blog series that introduces TensorFlow Datasets and Estimators. Part 1 focuse ...
- 【Elasticsearch全文搜索引擎实战】之Head插件实践
简介 Elasticsearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Ap ...
- U3D MonoBehaviour
一.简介 MonoBehaviour是每个脚本派生类的基类,它定义了一个脚本文件从最初被加载到最终被销毁的一个完整过程. 这个过程通过对应的方法体现出来,在不同的方法完成不同的功能,我们把这些方法称为 ...
- numpy使用指南
numpy.array numpy.array是numpy中用于处理n阶数组的对象,是其类族中的重要基类. numpy.array可以表示任意维的数组,可以使用构造函数初始化: arr = numpy ...
- ViewModel处理View相关事件的多种方式(非技术贴,仅学习总结)
众所周知,在UWP中,微软为我们提供了一种新的绑定方式:x:bind,它是基于编译时的绑定.在性能方面,运行时绑定Binding与它相比还是有些逊色的.因此针对一些确定的.不需要变更的数据,我们完全有 ...
- Java集合之Hashtable源码分析
概述 Hashtable也是基于哈希表实现的, 与map相似, 不过Hashtable是线程安全的, Hashtable不允许 key或value为null. 成员变量 Hashtable的数据结构和 ...
- Python编程-从入门到实践 Eric Matthes 著 袁国忠 译 - - 第二章 动手试一试
因为第一章的动手试一试基本都是探索性的,所以直接进入第二章. # 2.2 动手试一试 # 2_1 简单消息: 将一条消息存储到变量中,再将其打印出来. message = 'python 编程从入门到 ...
- Spring@Autowired注解与自动装配(转发)
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- 为Hexo Next主题添加分享及打赏功能
博客地址:往事亦如风的博客 要想先看打赏和分享功能效果,请移步我的博客 打赏功能 因为next主题自带打赏功能,所以我们只需要在next的主题配置文件中找到如下代码,image文件夹是我在blog/s ...