# Recommended: "php pdo class methods include: 1. exec method; 2. query method; 3. lastInsertId method; 4. setAttribute method, etc.
PHP Video Tutorial》
Common methods of pdo class:
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}的数据插入成功!"; } ?>
Set the get mode attribute
<?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 '
***
Get the index array
<?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 '
The above is the detailed content of What are the methods of php pdo class. For more information, please follow other related articles on the PHP Chinese website!