php pdo類別方法有:1、exec方法;2、query方法;3、lastInsertId方法;4、setAttribute方法等等。
#推薦:《PHP影片教學》
pdo類別常用方法:
exec()
query()
lastInsertId()
<?php $servername = "localhost"; $username = "root"; $password = "133nubia022"; $dbname='myweb'; $pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $pdo->exec('set names utf8'); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_BOTH); $sql="insert into user(username,age) values('user123','55')"; if($pdo->exec($sql)){ $lastid=$pdo->lastInsertId(); echo "ID为{$lastid}的数据插入成功!"; } ?>
• setAttribute()
#設定取得模式屬性
<?php $servername = "localhost"; $username = "root"; $password = "133nubia022"; $dbname='myweb'; $pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $pdo->exec('set names utf8'); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); $sql="select * from user"; $smt=$pdo->query($sql); $rows=$smt->fetchAll(); echo '<pre class="brush:php;toolbar:false">'; print_r($rows); echo '
##***
取得索引數組
<?php $servername = "localhost"; $username = "root"; $password = "133nubia022"; $dbname='myweb'; $pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $pdo->exec('set names utf8'); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_NUM); $sql="select * from user"; $smt=$pdo->query($sql); $rows=$smt->fetchAll(); echo '<pre class="brush:php;toolbar:false">'; print_r($rows); echo '
#取得混合數組
<?php $servername = "localhost"; $username = "root"; $password = "133nubia022"; $dbname='myweb'; $pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $pdo->exec('set names utf8'); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_BOTH); $sql="select * from user"; $smt=$pdo->query($sql); $rows=$smt->fetchAll(); echo '<pre class="brush:php;toolbar:false">'; print_r($rows); echo '
以上是php pdo 類別 方法有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!