①PDO方式连接 数据库
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <?php 9 //定义数据源 10 $dsn="mysql:dbname=test2;host=localhost"; 11 //$dsn="sqlsrv:dbname"; 12 //造pdo对象 13 $pdo=new PDO($dsn,"root","123"); 14 15 //写SQL语句 16 $sql="select * from Info"; 17 18 //准备执行语句 19 $st=$pdo->prepare($sql); 20 21 //执行预处理语句 22 $st->execute();// 条件判断 返回true 或 false 23 if ($st->execute()) { 24 print_r($st->fetch()); //一次调用一条 25 } 26 else{ 27 echo "执行失败!"; 28 }
//从结果集中取所有数据,返回二维数组
//print_r($st->fetchAll(PDO::FETCH_NUM));
//从结果集中取一条数据中的某一列,返回字符串
//var_dump($st->fetchColumn(1));
//从结果集中取一条数据,返回一个实体类的对象
//var_dump($st->fetchObject());
29 ?> 30 </body> 31 </html>
图:
② ????? prepare($sql) bindParam(1,$XX) $XX="ss" exectue() 方式
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <?php 9 //造PDO对象 10 $pdo=new PDO("mysql:dbname=test2;host=localhost","root","123"); 11 12 //写SQl语句 13 $sql="insert into Info values(?,?,?,?,?)"; 14 15 //准备SQL语句 16 $st=$pdo->prepare($sql); 17 //给SQL语句分配变量 18 $st->bindParam(1,$code); 19 $st->bindParam(2,$name); 20 $st->bindParam(3,$sex); 21 $st->bindParam(4,$nation); 22 $st->bindParam(5,$birthday); 23 //给变量赋值 24 $code="p120"; 25 $name="回家"; 26 $sex=true; 27 $nation="n002"; 28 $birthday="1988-9-6"; 29 //执行SQL语句 30 $st->execute(); 31 ?> 32 </body> 33 </html>
????? prepare($sql) exectue(array("ss")) 方式
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <?php 9 //造PDO对象 10 $pdo=new PDO("mysql:dbname=test2;host=localhost","root","123"); 11 12 //写SQl语句 13 $sql="insert into Info values(?,?,?,?,?)"; 14 15 //准备SQL语句 16 $st=$pdo->prepare($sql); 17 $st->execute(array('p0030','克隆',true,'n001','1989-2-3'));//把分配变量和赋值干掉,只用这句。 18 19 20 ?> 21 </body> 22 </html>
③::::: prepare($sql) bindParam("",$XX,PDO::PARAM_STR); $XX="" exectue() 方式
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <?php 9 //造PDO对象 10 $pdo=new PDO("mysql:dbname=test2;host=localhost","root","123"); 11 12 //写SQl语句 13 $sql="insert into info values(:c,:n,:s,:na,:b)"; 14 15 //准备SQL语句 16 $st=$pdo->prepare($sql); 17 18 //绑定参数 19 $st->bindParam("c",$code,PDO::PARAM_STR); 20 $st->bindParam("n",$name,PDO::PARAM_STR); 21 $st->bindParam("s",$sex,PDO::PARAM_STR); 22 $st->bindParam("na",$nation,PDO::PARAM_STR); 23 $st->bindParam("b",$birthday,PDO::PARAM_STR); 24 25 26 //参数赋值 27 $code="p181"; 28 $name="纳"; 29 $sex=true; 30 $nation="n002"; 31 $birthday="1988-2-3"; 32 33 //执行 34 $st->execute(); 35 ?> 36 </body> 37 </html>
::::: prepare($sql) execute(); 方式
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <?php 9 //造PDO对象 10 $pdo=new PDO("mysql:dbname=test2;host=localhost","root","123"); 11 12 //写SQl语句 13 $sql="insert into info values(:c,:n,:s,:na,:b)"; 14 15 //准备SQL语句 16 $st=$pdo->prepare($sql); 17 $st->execute(array('c'=>'p190','n'=>'水果','s'=>true,'na'=>'n002','b'=>'1990-2-3')); 18 ?> 19 </body> 20 </html>
图: