7.19 PDO (PHP Data object-php object) Database abstraction Layer

Source: Internet
Author: User
Tags prepare rowcount sql injection first row

<?php
Header ("content-type:text/html; Charset=utf-8 ");
PDO Operation MySQL
/* $pdo =new PDO ("Mysql:host=localhost;dbname=ceshi", "Root", "");
$pdo->exec ("Set names UTF8");//exec () a statement used to perform additions and deletions and settings
$sql = "Select *from student";
$YCL = $pdo->query ($sql);//query () is used to execute query statements
$attr = $ycl->fetchall (PDO::FETCH_ASSOC); */
Var_dump ($ATTR);
Lastinsertid () Usage: last inserted ID
/* $pdo =new PDO ("Mysql:host=localhost;dbname=ceshi", "Root", "");
$pdo->exec ("Set names UTF8");
$sql = "INSERT into student values (' ', ' ', ' zhangsan32 ', ', ', ')";
if ($pdo->exec ($sql)) {
$lastid = $pdo->lastinsertid ();
echo "ID {$lastid} data inserted successfully";
Var_dump ($lastid);
}*/
SetAttribute () Set some properties of the PDO
/* $pdo =new PDO ("Mysql:host=localhost;dbname=ceshi", "Root", "");
$pdo->exec ("Set names UTF8");
$pdo->setattribute (PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);//sets the type of query data returned so that you do not have to write fetchall every time (PDO:: FETCH_ASSOC).
$sql = "SELECT * from student";
$YCL = $pdo->query ($sql);
$attr = $ycl->fetchall (PDO::FETCH_ASSOC); omit this sentence
$attr = $ycl->fetchall ();
Var_dump ($ATTR); * *

Prepare () Usage
/* $pdo = new PDO (' Mysql:host=localhost;dbname=ceshi ', "root", "");
$pdo->exec ("Set names UTF8");
$sql = "SELECT * from student";
$YCL = $pdo->prepare ($sql);//preprocessing, prepare SQL statements to prevent SQL injection, but also improve the efficiency of the query, when another user requests the same SQL, will first find this prepared SQL, execute the query
$YCL->execute ();//execution
$attr = $ycl->fetchall (PDO::FETCH_ASSOC); */

Bindvalue ()
/* $pdo = new PDO (' Mysql:host=localhost;dbname=ceshi ', "root", "");
$pdo->exec ("Set names UTF8");
$sql = "SELECT * from student where Sno =?"; /prevent everyone's choice of sno different and cause duplicate SQL, set sno=?
$YCL = $pdo->prepare ($sql);//pretreatment
$id = 098;//This ID is passed over the parameter
$ycl->bindvalue (1, $id, PDO::P aram_int);//Bind $id to the first question mark and set type PDO to $id::P aram_int
$YCL->execute ();//execution
$attr = $ycl->rowcount ();
Var_dump ($ATTR); * *
ColumnCount () returns the number of columns in a table
/* $pdo = new PDO (' Mysql:host=localhost;dbname=ceshi ', "root", "");
$pdo->exec ("Set names UTF8");
$sql = "SELECT * from student";
$YCL = $pdo->prepare ($sql);//pretreatment
$YCL->execute ();//execution
$attr = $ycl->columncount ();
$attr = $ycl->fetchcolumn (3);
$attr = $ycl->fetchcolumn (4); The value of the nth (n from 0) column of a row of data is queried from the preprocessing, the first row is executed, the second is the second row
Var_dump ($attr); *///rowcount () Number of rows affecting data
/* $sql = "SELECT * from student";
$YCL = $pdo->prepare ($sql);
$YCL->execute ();
$attr = $ycl->rowcount ();
Var_dump ($ATTR); * *
PDO pretreatment
/* $pdo = new PDO (' Mysql:host=localhost;dbname=ceshi ', "root", "");
$pdo->exec ("Set names UTF8");
Preprocessing of Queries
$sql = "SELECT * from student";
$YCL = $pdo->prepare ($sql);
$YCL->execute ();
$attr = $ycl->fetchall (PDO::FETCH_ASSOC);
Var_dump ($ATTR); * *

7.19 PDO (PHP Data object-php object) Database abstraction Layer

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.