Home  >  Article  >  Backend Development  >  Simple example of PDO operation in PHP, simple example of pdo operation_PHP tutorial

Simple example of PDO operation in PHP, simple example of pdo operation_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:55:20810browse

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.

The

class 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117057.htmlTechArticle Simple example of PHP PDO operation, simple example of pdo operation. This article describes the simple PDO operation of PHP. I would like to share it with you for your reference. The details are as follows: There is a lot of information about PDO on the Internet. This...
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