<?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