PDO操作
2015-10-13 15:37
169 查看
1.创建实例与取结果集
2.取一个字段结果
3.prepare
4.检查链接
5.返回值
<? $db = new PDO('mysql:host=localhost;dbname=test', $user, $pass); $rs = $db->query("SELECTaa,bb,cc FROM foo"); while ($arr = $rs->fetch()) { //... } ?>
2.取一个字段结果
<?php $rs = $db->query("SELECT COUNT(*) FROM foo"); $col = $rs->fetchColumn(); echo $col; ?>
3.prepare
<?php /* Execute a prepared statement by passing an array of values */ $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ?'); $sth->execute(array(150, 'red')); $red = $sth->fetchAll(); $sth->execute(array(175, 'yellow')); $yellow = $sth->fetchAll(); ?>
4.检查链接
<?php try { $db = new PDO('mysql:host=localhost;dbname=test', $user, $pass); $db = null; } catch (PDOException $e) { print "Error: " . $e->getMessage() . "<br/>"; die(); } ?>
5.返回值
PDO::exec() - Execute an SQL statement and return the number of affected rows //返回受影响的行数 PDO::query() - Executes an SQL statement, returning a result set as a PDOStatement object //返回结果集对象
$rs = $db->query("SELECT * FROM test_table"); echo $rs->rowCount(); //取结果集对象的行数
相关文章推荐
- Eclipse的tomcat插件
- python学习001
- cd命令详解
- spring boot 框架下的redis缓存问题
- Zimbra管理手册
- HttpClient请求服务器代码优化版
- soundtouch变速wsola算法之改进
- hdu 4738 Caocao's Bridges 图的割边
- maven环境配置
- HttpClient请求服务器代码优化版
- Slow count(*) 绕开源代码解决 通过外部解决
- 审核流(3)低调奢华,简单不凡,实例演示-SNF.WorkFlow--SNF快速开发平台3.1
- Win10 Build 10565 Edge浏览器更新日志和已知问题
- String 转 BigDecimal
- 静态链表的学习
- 从头到尾彻底理解KMP
- 数据库索引
- Redis 集群方案
- ZCS 开源版管理员指南
- setDescendantFocusability,在父View和子View间处理焦点关系