Home >Backend Development >PHP Problem >What are the methods of php pdo class
php pdo class methods: beginTransaction(), commit(), __construct(), errorCode(), errorInfo(), exec(), getAttribute(), query(), quote(), etc.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
What is PDO
PDO is the abbreviation of PHP Date Object (PHP Data Object). It is a lightweight and consistent interface defined by PHP for accessing the database. It provides a data access abstraction. layer, so that no matter what database you use, you can perform queries and obtain data through the same function, which greatly simplifies database operations and can shield differences between different databases.
PDO is released together with PHP5.1 version. Using PDO can easily develop cross-database programs and transplant between different databases. Currently, the databases supported by PDO include Firebird, FreeTDS, Interbase, and MySQL. , SQL Server, ODBC, Oracle, Postgre SQL, SQLite and Sybase, etc.
With PDO, you no longer need to use a series of functions of mysqli_*. You only need to use the methods in PDO to operate the database.
Methods of the PDO class:
beginTransaction() — Start a transaction
commit() — Commit a transaction
__construct() — Create a PDO instance representing a database connection
errorCode() — Obtain the SQLSTATE related to the last operation of the database handle
errorInfo() — Return the error information of the last operation of the database
exec() — Execute a SQL statement and return the number of affected rows
getAttribute() — Retrieve the attributes of a database connection
getAvailableDrivers( ) — Returns an array of available drivers
inTransaction() — Checks if within a transaction
lastInsertId() — Returns the last inserted row The ID or sequence value
prepare() — Prepares the SQL statement to be executed and returns a PDOStatement object
Commonly used methods of the pdo class:
<?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}的数据插入成功!"; } ?>
<?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 ''; ?>
Get mixed 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_BOTH); $sql="select * from user"; $smt=$pdo->query($sql); $rows=$smt->fetchAll(); echo '<pre class="brush:php;toolbar:false">'; print_r($rows); echo ''; ?>
Recommended learning: "PHP Video Tutorial
》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!