Data Access PDO Abstraction Layer
Method One: Compare simple points:
<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP//the DSN format is: Driver name: dbname= database name; host= server address$dsn= "Mysql:dbname=mydb;host=localhost"; //Making PDO Objects$pdo=NewPDO ($dsn, "Root", ""); //Write SQL statements$sql= "SELECT * FROM Info"; /*//Execute SQL statement can but generally not $v = $pdo->query ($sql),//The object returned is very special, you can directly loop through the foreach ($v as $a) {echo $a [0];}*/ //preprocessing, first put the SQL statement on the server waiting for us to give him instructions to execute//prepare an SQL statement, return the PDO object$stm=$pdo->prepare ($sql); //executes the prepared SQL statement, returns true successfully, fails to return falseif($stm-Execute ()) { //the output is//1. Row-by-line data//var_dump ($stm->fetch ()),//Read the data has an index also has an associative array,//inside can write parameters//var_dump ($stm->fetch (pdo::fe TCH_ASSOC));//associative array//var_dump ($stm->fetch (Pdo::fetch_both));////var_dump ($stm->fetch (pdo::fetch_num)); /indexed array//2. Read all data//var_dump ($stm->fetchall ());//parameter same as//3. Read a column//var_dump ($stm->fetchcolumn (1 );//inside is index//4. Returns the object//var_dump ($stm->fetchobject ());}Else{ Echo"Execution Failed"; }?> </body>
Note the output mode in PDO:
read data row by line----- $stm->fetch (); #包含索引数组和关联数组
Read all data-----$stm->fetchall (); #包含索引数组和关联数组
The parentheses can write the parameters, the two methods are the same, the common is:
$stm->fetch (PDO::FETCH_ASSOC); #关联数组
$stm->fetch (Pdo::fetch_both); #都有
$stm->fetch (Pdo::fetch_num); #索引数组
Returns a column-----$stm->fetchcolumn (1); #里面是索引
Returns the object-------$stm->fetchobject ();
Method 2: To add data examples, divided into two types, indexed arrays and associative arrays
Indexed arrays:
<title>pdo Method 2 finishing </title>PHP//indexed Array//Build Object$dsn= "Mysql:dbname=mydb;host=localhost";$pdo=NewPDO ($dsn, "Root", ""); //Write SQL statement//$sql = "INSERT into Nation values (?,?)";//preprocessing$stm=$pdo->prepare ($sql); //parameter binding value, more cumbersome not recommended/*$stm->bindparam (1, $code); $stm->bindparam (2, $name); $code = "P002"; $name = "Back K";*/ //Build Arrays$attr=Array("p111", "Zhang San"); //Executionif($stm->execute ($attr)){ Echo"Add Success"; }Else{ Echo"Add Failed"; }?> </body>Associative Arrays<title>pdo Method 2 Finishing </title>PHP//associative arrays//Build Objects$dsn= "Mysql:dbname=mydb;host=localhost";$pdo=NewPDO ($dsn, "Root", ""); //Write SQL statements$sql= "INSERT into Nation values (: Code,:name)"; //pretreatment$stm=$pdo->prepare ($sql); //bind value to parameter, trouble not to recommend/*$stm->bindparam ("code", $code, PDO::P aram_str); $stm->bindparam ("name", $name, PDO::P aram_str); $code = " P003 "; $name =" AAAA ";*/ //Build Arrays$attr=Array("Code" = "p005", "name" = "BBBB"); //Executionif($stm->execute ($attr)){
echo "Add success";
}
Else
{
Echo "Add failed"; }? > </body>
Another way is to use the form, more convenient, all can use
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head> <Body><formAction= "pdo2.php"Method= "POST"><Div>Code:<inputtype= "text"name= "Code" /></Div><BR/> <Div>Name:<inputtype= "text"name= "Name" /></Div><BR/> <Div><inputtype= "Submit"value= "Submit" /></Div></form></Body></HTML>
<title>pdo Method 2 Finishing </title>PHP//associative arrays//Build Objects$dsn= "Mysql:dbname=mydb;host=localhost";$pdo=NewPDO ($dsn, "Root", ""); //Write SQL statements$sql= "INSERT into Nation values (: Code,:name)"; //pretreatment$stm=$pdo->prepare ($sql); //Executionif($stm->execute ($_post)){ Echo"Add Success";}Else{ Echo"Add Failed"; }?></body>
PHP Data Access Data layer