try {
$dbh = new PDO('mysql:host=localhostdbname=test', $user, $pass)
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row)
}
$dbh = null//释放资源
} catch (PDOException $e) {
print "错误!: " . $e->getMessage() . "<br></br>"
die()
}
//执行删除 *** 作,插入、修改与此类似,更改SQL语句即可
$count = $dbh->exec("DELETE FROM fruit WHERE colour = 'red'")
$sql = 'select count(*) from tb_root'$result = $pdo->query($sql)//提交sql
$rowsNumber = $result->fetchColumn()//取回结果集中的一个字段
echo $rowsNumber
query方法用来提交select语句,exec方法一般用于insert,update, delete等
对于普通的查询,可以这样做
$sql = 'select * from tb_root'
$result = $pdo->query($sql)
$rows = $result->fetchAll(PDO:FETCH_ASSOC)//取得所有行
$rowsNumber = count($rows)
echo $rowsNumber
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)