PDO操作

1.创建实例与取结果集

<?
$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();     //取结果集对象的行数

 

转载于:https://www.cnblogs.com/fenle/p/4874800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值