<?
$pdo = new PDO('mysql:host=localhost;port=3336;dbname=TD_OA', 'root', 'myoa888');
//$pdo = new PDO('mysql:host=localhost;port=3336;dbname=TD_OA', 'root', 'myoa888','array(PDO::ATTR_PERSISTENT => true)'); //持久连接
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //错误的话抛出异常
$pdo->exec('set names gb2312');
$query="select user_id,uid,user_name from user";
$rs = $pdo -> query($query);
while($row = $rs -> fetch()){
//echo $row["user_id"]."<br>";
//print_r($row);
}
$num=$rs->rowCount();
echo $num."<br>";//查询个数
$rs1 = $pdo -> query($query);
$row1=$rs1->fetchAll();
foreach ($row1 as $rs)
{
$id = $rs['user_id'];
// echo $id."<br>";
}
// echo "<pre>";
//print_r($row1);
// echo "</pre>";
$num1=$rs1->rowCount();
echo $num1."<br>";//查询个数
$rs2 = $pdo->query("SELECT user_name FROM user where user_id='admin'");//查询唯一的最后一个的某一列
$col = $rs2->fetchColumn();
echo $col."<br>";
$query="select * from company_sun where company='".$zfjg."' and leibie_id='".$i."' and leibie='".$leibie."'";
//echo $query."<br>";
$res = $pdo->prepare($query);
//$res->exec();
$res->execute();
$num_rows=$res->rowCount();//取数
//修改
$query3="update user set user_name='系统管理员' where user_id='admin'";
$stmt = $pdo->prepare($query3);
$stmt->execute();
echo $stmt->rowCount()."<br>"; //返回影响的行数
$query4="update user set user_name='系统管理员' where user_id='admin'";
$stmt4 = $pdo->exec($query4); // 返回影响的行数了
echo $stmt4."<br>";
/*删除*/
$sql = "DELETE FROM `user` WHERE `user_id` LIKE 'kevin_'"; //kevin%
$stmt = $pdo->prepare($sql);
$stmt->execute();
echo $stmt->rowCount()."<br>";//删除同修改
//新增
$pdo -> exec("insert into db_demo(name,content) values('title','content')");
echo "插入成功!";
echo $pdo -> lastinsertid();
/*添加*/
//$sql = "INSERT INTO `user` SET `login`=:login AND `password`=:password";
$sql = "INSERT INTO `user` (`login` ,`password`)VALUES (:login, :password)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':login'=>'kevin2',':password'=>''));
echo $pdo->lastinsertid();
//新增
$sql = "INSERT INTO lailu (ip,times) VALUES ('".$iipp."', '". date('Y-m-d H:i:s')."')";
$stmt = $pdo->prepare($sql);
$stmt->execute();
echo $pdo->lastinsertid();
?>
PDO::query() 主要是用于有记录结果返回的操作,特别是SELECT操作
PDO::exec() 主要是针对没有结果集合返回的操作,如INSERT、UPDATE等操作
PDO::lastInsertId() 返回上次插入操作,主键列类型是自增的最后的自增ID
PDOStatement::fetch() 是用来获取一条记录
PDOStatement::fetchAll() 是获取所有记录集到一个中