Les méthodes de classe php pdo incluent : 1. méthode exec ; 2. méthode query ; 3. méthode lastInsertId 4. méthode setAttribute, etc.
Recommandé : "Tutoriel vidéo PHP》
Méthodes courantes de la classe 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()
Définir l'attribut du mode d'obtention
<?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 '
***
Obtenir un tableau d'index
<?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 '
Obtenir un tableau mixte
<?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 '
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!