PDO DEMO in PHP

Source: Internet
Author: User
Tags dsn php database

PDO = "PHP DATABASE OBJECT"

1. Select

$dsn= "Mysql:host=127.0.0.1;port=3306;dbname=dbname"; $DBH=NewPDO ($dsn, ' root ', ' password ');$sql= "SELECT * FROM table_name WHERE name =: Name and PWD =:p WD";$sth=$DBH->prepare ($sql);$sth->bindvalue (': Name ', ' user ');$sth->bindvalue (':p wd ', ' password ');$sth-execute ();foreach($sth  as $row) {     Echo Var_dump($row); } $DBH=NULL;
$dsn= "Mysql:host=127.0.0.1;port=3306;dbname=dbname"; $DBH=NewPDO ($dsn, ' root ', ' password ');$sql= "SELECT * FROM table_name WHERE name =?" and PWD =? ";$sth=$DBH->prepare ($sql);$sth->bindvalue (1, ' user ');$sth->bindvalue (2, ' password ');$sth-execute ();foreach($sth  as $row) {     Echo Var_dump($row); } $DBH=NULL;

2. UPDATE

$dsn= "Mysql:host=127.0.0.1;port=3306;dbname=dbname"; $DBH=NewPDO ($dsn, ' root ', ' password ');$sql= "UPDATE table_name set name =: name WHERE id =: id";$sth=$DBH->prepare ($sql);$sth->bindvalue (': Name ', ' user ');$sth->bindvalue (': Id ', ' 1 ');$flag=$sthExecute ();//true or False$DBH=NULL;
$dsn= "Mysql:host=127.0.0.1;port=3306;dbname=dbname"; $DBH=NewPDO ($dsn, ' root ', ' password ');$sql= "UPDATE table_name set name =?" WHERE id =? ";$sth=$DBH->prepare ($sql);$sth->bindvalue (1, ' user ');$sth->bindvalue (2, ' 1 ');$flag=$sthExecute ();//true or False$DBH=NULL;

3. Insert

$dsn= "Mysql:host=127.0.0.1;port=3306;dbname=dbname"; $DBH=NewPDO ($dsn, ' root ', ' password ');$sql= "INSERT into table_name (name) VALUES (: Name)";$sth=$DBH->prepare ($sql);$sth->bindvalue (': Name ', ' user ');$flag=$sthExecute ();//true or False$DBH=NULL;
$dsn= "Mysql:host=127.0.0.1;port=3306;dbname=dbname"; $DBH=NewPDO ($dsn, ' root ', ' password ');$sql= "INSERT into table_name (name) VALUES (?)";$sth=$DBH->prepare ($sql);$sth->bindvalue (1, ' user ');$flag=$sthExecute ();//true or False$DBH=NULL;

4. Delete

$dsn= "Mysql:host=127.0.0.1;port=3306;dbname=dbname"; $DBH=NewPDO ($dsn, ' root ', ' password ');$sql= "DELETE from table_name WHERE id =: id";$sth=$DBH->prepare ($sql);$sth->bindvalue (': Id ', ' 1 ');$flag=$sthExecute ();//true or False$DBH=NULL;
$dsn= "Mysql:host=127.0.0.1;port=3306;dbname=dbname"; $DBH=NewPDO ($dsn, ' root ', ' password ');$sql= "DELETE from table_name where id =?";$sth=$DBH->prepare ($sql);$sth->bindvalue (1, ' 1 ');$flag=$sthExecute ();//true or False$DBH=NULL;

The second piece of code in each section is used to bind parameters in the same way as the number index, and some people may not understand the difference between the post-binding and the direct generation of the SQL statement, but it is not difficult to understand.

Suppose you enter:

Select *  from where = ?

The part of the question mark can become this way if it is generated directly

Select *  from where = 1 or 1 = 1

If you bind dynamically, the question mark section is limited to entering a variable that conforms to the ID field type, and if there is SQL injection it compiles

PDO DEMO in PHP

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.