abstrait:<?php//pdo中的新增操作//1.創(chuàng)建pdo對(duì)象,連接數(shù)據(jù)庫(kù)$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');//2.創(chuàng)建SQL語(yǔ)句$sql = "INSERT INTO `staff` (`name`,`age`,`sex`,`position`,`
<?php
//pdo中的新增操作
//1.創(chuàng)建pdo對(duì)象,連接數(shù)據(jù)庫(kù)
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
//2.創(chuàng)建SQL語(yǔ)句
$sql = "INSERT INTO `staff` (`name`,`age`,`sex`,`position`,`mobile`,`hiredate`,`is_show`) VALUES (:name,:age,:sex,:position,:mobile,:hiredate,:is_show)";
//3.創(chuàng)建預(yù)處理對(duì)象
$stmt = $pdo->prepare($sql);
//4.參數(shù)綁定
$name = '張三';
$age = 25;
$sex =1;
$position = '經(jīng)理';
$mobile = '13311112222';
$hiredate = time();
$isShow = 1;
$stmt->bindParam(':name',$name,PDO::PARAM_STR,20);
$stmt->bindParam(':age',$age,PDO::PARAM_INT);
$stmt->bindParam(':sex',$sex,PDO::PARAM_INT);
$stmt->bindParam(':position',$position,PDO::PARAM_STR,30);
$stmt->bindParam(':mobile',$mobile,PDO::PARAM_STR,11);
$stmt->bindParam(':hiredate',$hiredate,PDO::PARAM_INT);
$stmt->bindParam(':is_show',$isShow,PDO::PARAM_INT);
//5.執(zhí)行添加
if($stmt->execute()){
echo ($stmt->rowCount()>0) ? '成功添加了 ' . $stmt->rowCount() . ' 條記錄!' : '沒(méi)有記錄被添加';
}else {
exit(print_r($stmt->errorInfo(),true));
}
<?php
//pdo中的更新操作
//1.創(chuàng)建pdo對(duì)象,連接數(shù)據(jù)庫(kù)
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
//2.創(chuàng)建SQL語(yǔ)句
$sql = "UPDATE `staff` SET `age` = :age,`position` = :position,`mobile` = :mobile WHERE `id` = :id";
//3.創(chuàng)建預(yù)處理對(duì)象
$stmt = $pdo->prepare($sql);
//4.參數(shù)綁定
$id = 43;
$age = 28;
$position = '總經(jīng)理';
$mobile = '13311113333';
$stmt->bindParam(':age',$age,PDO::PARAM_INT);
$stmt->bindParam(':id',$id,PDO::PARAM_INT);
$stmt->bindParam(':position',$position,PDO::PARAM_STR,30);
$stmt->bindParam(':mobile',$mobile,PDO::PARAM_STR,11);
//5.執(zhí)行更新
if($stmt->execute()){
echo ($stmt->rowCount()>0) ? '成功更新了 ' . $stmt->rowCount() . ' 條記錄!' : '沒(méi)有記錄被更新';
}else {
exit(print_r($stmt->errorInfo(),true));
}
<?php
//pdo中的刪除操作
//1.創(chuàng)建pdo對(duì)象,連接數(shù)據(jù)庫(kù)
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
//2.創(chuàng)建SQL語(yǔ)句
$sql = "DELETE FROM `staff` WHERE `id` = :id";
//3.創(chuàng)建預(yù)處理對(duì)象
$stmt = $pdo->prepare($sql);
//4.參數(shù)綁定
$id = 38;
$stmt->bindParam(':id',$id,PDO::PARAM_INT);
//5.執(zhí)行刪除
if($stmt->execute()){
echo ($stmt->rowCount()>0) ? '成功刪除了 ' . $stmt->rowCount() . ' 條記錄!' : '沒(méi)有記錄被刪除';
}else {
exit(print_r($stmt->errorInfo(),true));
}
Professeur correcteur:查無(wú)此人Temps de correction:2019-06-12 09:19:06
Résumé du professeur:完成的不錯(cuò)。數(shù)據(jù)庫(kù)就是增刪查改,數(shù)據(jù)的操作。繼續(xù)加油。