PHP's PDO operation Simple example, PDO operation Simple example _php tutorial

Source: Internet
Author: User

A simple example of the PDO operation in PHP, a simple example of PDO operation


The examples in this article describe simple PDO operations for PHP. Share to everyone for your reference, as follows:

There is a lot of information about PDO online. It's not a liability here.

Here I encapsulate all the PDO operations into one class for ease of operation.

The class code is as follows:

Class DB {  //pdo object 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" ta Ble1 ' 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 table type InnoDB (Support transaction) MyISAM (transaction not supported)  echo $db->con->rollback ();  throw new MyException ("Transaction test Error", $e);} $db = NULL;
  

PDO supports SQL statements to be invoked as arguments, effectively preventing SQL injection.

More interested in PHP related content readers can view this site: "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", " PHP Date and Time usage summary, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL Database Operations Primer, and PHP Common database operations Tips Summary

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP uses PDO to manipulate MySQL DB instances
    • PHP implementation of PDO MySQL database operation class
    • PHP PDO Operations Summary
    • New PDO Database operation PHP version (MySQL only)
    • Summary of differences in the PDO database operation between PHP 5.1.* and 5.2.*
    • A simple way to use PDO in PHP5.2
    • Php simple method of using PDO in the database abstraction layer
    • A detailed description of the PDO class in PHP
    • The method of PDO in PHP to realize the deletion and modification of database

http://www.bkjia.com/PHPjc/1117057.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117057.html techarticle A simple example of the PDO operation in PHP, a simple PDO operation example describes the simple PDO operation of PHP. Share to everyone for your reference, as follows: Online about PDO a lot of information. This ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.