The examples in this article describe the PHP data object PDO operation skills. Share it with everyone for your reference, the details are as follows:
PHP Data Object (PDO) extension defines a lightweight consistent interface for PHP to access the database.
<?php try { $dsn = "mysql:host=localhost; port=3306; dbname=wsq_hotel; charset=utf-8"; $user = 'root'; $psw ='root'; $pdo = new PDO($dsn,$user,$psw); $sql = 'select goods_prices from wsq_goods_info where goods_id=2'; // $sql = "show database"; $res = $pdo->query($sql) or var_dump($pdo->errorInfo()); // var_dump($res); $mon = $res->fetch(PDO::FETCH_ASSOC); echo $mon['goods_price']; } catch (PDOException $e) { echo $e->getMessage(); } ?>
PDO operation transaction
//开启事务 beginTransacition() //回滚 rollback() //提交 commit() //判断是否处于事务之中 inTransaction()
Return the ID of the last inserted row
PDO::lastInsertID()
exec() execution
Compared with query(), exec() Returns the number of affected rows
$sql = "insert into table values('$val')"; if(false===$pdo->exec($sql)){ echo '执行失败'; }
PDO implements pre-compilation
Refers to a syntax for executing sql that pre-compiles the sql structure
If multiple structures are executed For the same SQL, the intermediate result (syntax tree) of compilation should also be consistent, so the same structure can be compiled uniformly and executed with different data each time.
Compile unified structure
$pdoStatement = $pdo->prepare(sql结构)
Bind data to intermediate compilation results
$pdoStatement ->bindValue()
Execution
$pdoStatement ->execute() //$sql = "insert into table values(null,?)"; $sql = "insert into table values(null,:name)"; $stmt = $pdo->prepare($sql); //多组数据也是一编译一执行 //$stmt->bindValue(1,'bee'); $stmt->bindValue(':name','bee'); $res = $stmt->execute(); var_dump($res);
Pre-compilation can better prevent sql injection, yes Because the user's data is not required to participate in pre-compilation, the structure is fixed during compilation, so the data does not affect the SQL structure.
$pdo->query() and $pdo->execute() If you need to prevent sql injection, you can use $pdo->quote() (its function is to escape first and then add quotes)
Commonly used methods of PDOstatement:
errorInfo()
errorCode()
fetchColumn()
fetch()
fetchAll()
rowCount()
closeCursor()
pdo application
<?php header('content-type:text/html;charset=utf-8'); class PDODB{ static private $_init; private $_host; private $_port; private $_dbname; private $_username; private $_password; private $_charset; private $_dns; private $_pdo; private function __construct($config){ $this->_initParamas($config); $this->_initDNS(); $this->_initDriverOptions(); $this->_initPDO(); } private function __clone(){} static public function getInstance($config){ if(!static::$_init instanceof static){ static::$_init = new static($config); } return static::$_init; } private function _initParamas($config){ $this->_host = isset($config['host'])?$config['host']:'localhost'; $this->_port = isset($config['port'])?$config['port']:'3306'; $this->_dbname = isset($config['dbname'])?$config['dbname']:''; $this->_username = isset($config['username'])?$config['username']:'root'; $this->_passward = isset($config['passward'])?$config['passward']:''; $this->_charset = isset($config['charset'])?$config['charset']:'utf8'; } private function _initDNS(){ $this->_dns = "mysql:host=$this->_host;port=$this->_port;dbname=$this->_dbname"; } private function _initDriverOptions(){ $this->_driverOptions = array( PDO::MYSQL_ATTR_INIT_COMMAND => "set names $this->_charset" ); } private function _initPDO(){ $this->_pdo = new PDO($this->_dns,$this->_username,$this->_passward,$this->_driverOptions) or die("fail"); } public function query($sql){ if(!$result = $this->_pdo->query($sql)){ $erro = $this->_pdo->errorInfo(); echo '失败的语句'.$sql.'<br>'; echo '错误代码'.$erro[1].'<br>'; echo '错误信息'.$erro[2].'<br>'; die; } return $result; } public function fetchAll($sql){ $res = $this->query($sql); $list = $res->fetchAll(PDO::FETCH_ASSOC); $res->closeCursor(); return $list; } public function fetchRow($sql){ $res = $this->query($sql); $row = $res->fetch(PDO::FETCH_ASSOC); $res->closeCursor(); return $row; } public function fetchOne($sql){ $res = $this->query($sql); $one = $res->fetchColumn(); $res->closeCursor(); return $one; } public function escape_string($data){ return $this->_pdo->quote($data); } } $config = array( "host"=>"localhost", "username"=>"root", "passward"=>"root", "dbname"=>"students" ); $pdo = PDODB::getInstance($config); $sql = "select sdept from student where sage=21"; var_dump($pdo->fetchRow($sql)); ?>
The running effect diagram is as follows:
I hope this article will be useful to everyone in PHP programming. helped.
For more articles related to summary of PHP data object PDO operation skills, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
