Home  >  Article  >  Backend Development  >  What are the methods of php pdo class

What are the methods of php pdo class

青灯夜游
青灯夜游Original
2021-07-15 11:25:001726browse

php pdo class methods: beginTransaction(), commit(), __construct(), errorCode(), errorInfo(), exec(), getAttribute(), query(), quote(), etc.

What are the methods of php pdo class

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

  • ##query() — Execute the SQL statement, returns a PDOStatement object, which can be understood as a result set

  • quote() — adds quotation marks to the string in the SQL statement.

  • rollBack() — Roll back a transaction

  • setAttribute() — Set an attribute

Commonly used methods of the pdo class:

    ##exec()
  • query()
  • lastInsertId()

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "133nubia022";
    $dbname=&#39;myweb&#39;;
    
    $pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $pdo->exec(&#39;set names utf8&#39;);
    
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_BOTH);
    
    $sql="insert into user(username,age) values(&#39;user123&#39;,&#39;55&#39;)";
    
    if($pdo->exec($sql)){
    	$lastid=$pdo->lastInsertId();
    	echo "ID为{$lastid}的数据插入成功!";
    }
    ?>

What are the methods of php pdo class

    ##setAttribute()
  • Set the get mode attribute

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname=&#39;myweb&#39;;
 

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

$pdo->exec(&#39;set names utf8&#39;);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);

$sql="select * from user";

$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($rows);
echo &#39;
'; ?>***

Get the index arrayWhat are the methods of php pdo class

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname=&#39;myweb&#39;;

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->exec(&#39;set names utf8&#39;);

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_NUM);

$sql="select * from user";
$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($rows);
echo &#39;
'; ?>

What are the methods of php pdo classGet mixed array

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname=&#39;myweb&#39;;

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->exec(&#39;set names utf8&#39;);

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_BOTH);

$sql="select * from user";
$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($rows);
echo &#39;
'; ?>

Recommended learning: "What are the methods of php pdo classPHP 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn