


Simple example of PDO operation in PHP, simple example of pdo operation_PHP tutorial
A simple example of PHP's PDO operation, a simple example of pdo operation
The example in this article describes the simple PDO operation of PHP. Share it with everyone for your reference, the details are as follows:
There is a lot of information about PDO on the Internet. No need to go into details here.
Here I encapsulate all PDO operations into a class for easy operation.
Theclass code is as follows:
class DB { //pdo对象 public $con = NULL; function DB() { $this->con = new PDO("mysql:host=127.0.0.1;dbname=dbtest", "root", "xxx", array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES `utf8`', PDO::ATTR_PERSISTENT => TRUE, )); $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->con->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); } public function query($sql, $para = NULL) { $sqlType = strtoupper(substr($sql, 0, 6)); $cmd = $this->con->prepare($sql); if($para != NULL) { $cmd->execute($para); } else { $cmd->execute(); } if($sqlType == "SELECT") { return $cmd->fetchAll(); } if($sqlType == "INSERT") { return $this->con->lastInsertId(); } return $cmd->rowCount(); } }
How to use:
include "pdo.php"; $db = new DB(); $subjectList = $db->query("SELECT * FROM `table1`"); $count = $db->query("UPDATE `table1` SET `name` = 'test' WHERE `id` = :id", array(':id' => 795)); try { echo $db->con->beginTransaction(); $count = $db->con->exec("UPDATE `table1` SET `name` = 'test1' WHERE `id` = 795"); $count = $db->con->exec("UPDATE `table1` SET `name1` = 'test22' WHERE `id` = 795"); $count = $db->con->exec("UPDATE `table1` SET `name1` = 'test333' WHERE `id` = 795"); echo $db->con->commit(); } catch (Exception $e) { // MYSQL 的表类型 InnoDB(支持事务) MyISAM(不支持事务) echo $db->con->rollBack(); throw new MyException("事务测试错误", $e); } $db = NULL;
PDO supports calling SQL statements as parameters, which can effectively prevent SQL injection.
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar", "Summary of PHP Office Document Skills (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary" 》
I hope this article will be helpful to everyone in PHP programming.
Articles you may be interested in:
- php uses PDO to operate MySQL database instance
- PHP implements PDO mysql database operation class
- PHP PDO operation summary
- New PDO database operation class php version (only applicable to Mysql)
- Summary of the differences in PDO database operation between PHP 5.1.* and 5.2.*
- A simple way to use PDO in PHP5.2
- A simple way to use PDO in the database abstraction layer in php
- Detailed explanation of the PDO class in PHP
- PDO method to implement database in php Check for additions, deletions, modifications

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
