标签:
<?php
// prepare();
// bindValue();
// bindParam();
// bindColumn();
// execute();
// rowCount();
try{
$pdo = new PDO("mysql:host=localhost;dbname=test;","root","");
}catch(PDOException $e){
die("database connect fail".$e->getMessage());
}
/*=== prepare: ? method
$sql = "insert into users(id,name,age) values(?,?,?)";
$stmt = $pdo->prepare($sql);
// 1. bindValue()
$stmt->bindValue(1,null);
$stmt->bindValue(2,‘test55‘);
$stmt->bindValue(3,22);
//2. bindParam()
$stmt->bindParam(1,$id);
$stmt->bindParam(2,$name);
$stmt->bindParam(3,$age);
$id = null;
$name = "test66";
$age = 33;
//3. array()
$stmt->execute(array(null,‘test77‘,22));
$stmt->execute();
echo $stmt->rowCount();
*/
/* 2. method 2
$sql = "insert into users(id,name,age) values(:id,:name,:age)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue("id",null);
$stmt->bindValue("name",‘ceshi1‘);
$stmt->bindValue("age",22);
$stmt->bindParam("id",$id);
$stmt->bindParam("name",$name);
$stmt->bindParam("age",$age);
$id = null;
$name = "ceshi2";
$age = 33;
$stmt->execute(array("id"=>null,"name"=>ceshi3,"age"=>66);
$stmt->execute();
echo $stmt->rowCount();
*/
// select method
$sql = "select id,name,age from users";
$stmt = $pdo->prepare($sql);
$stmt->execute();
// foreach($stmt as $row){
// echo $row[‘id‘]."-----".$row[‘name‘]."<br>";
// }
$stmt->bindColumn(1,$id);
$stmt->bindColumn(2,$name);
$stmt->bindColumn("age",$age);
while($row=$stmt->fetch(PDO::FETCH_COLUMN)){
echo "{$id}:{$name}:{$age}<br>";
}
标签:
原文地址:http://www.cnblogs.com/htmlphp/p/4847316.html