亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

使用PDO做成績表完成增加/更新/刪除操作

原創(chuàng) 2019-02-08 18:03:10 324
摘要:1、數(shù)據(jù)庫2、新增數(shù)據(jù)操作<?php //1.創(chuàng)建PDO對象,連接數(shù)據(jù)庫 $pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root'); //2.創(chuàng)建預(yù)處理對象STMT   //添加數(shù)據(jù) $sql =&

1、數(shù)據(jù)庫

QQ截圖20190208164519.jpg

2、新增數(shù)據(jù)操作

<?php
//1.創(chuàng)建PDO對象,連接數(shù)據(jù)庫
$pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root');

//2.創(chuàng)建預(yù)處理對象STMT
  //添加數(shù)據(jù)
$sql = "INSERT INTO `user` (`name`,`sex`,`age`,`fenshu`,`create_time`) VALUES (:name,:sex,:age,:fenshu,:create_time)";
//3.驗(yàn)證SQL語句,創(chuàng)建預(yù)處理對象
$stmt = $pdo->prepare($sql);

//4.參數(shù)綁定
$name = '張三';
$sex = 0;
$age = 18;
$fenshu = 85;
$createTime = time();

$stmt->bindParam(':name',$name,PDO::PARAM_STR,20);
$stmt->bindParam(':sex',$sex,PDO::PARAM_INT);
$stmt->bindParam(':age',$age,PDO::PARAM_INT);
$stmt->bindParam(':fenshu',$fenshu,PDO::PARAM_INT);
$stmt->bindParam(':create_time',$createTime,PDO::PARAM_INT);

//5.執(zhí)行添加
if($stmt->execute()){
    echo($stmt->rowCount()>0) ? '成功添加了'.$stmt->rowCount().'條記錄' : '沒有記錄被增加';
} else {
    exit(print_r($stmt->errorInfo(),true));
}

QQ截圖20190208164519.jpg

3、更新數(shù)據(jù)操作

<?php
//1.創(chuàng)建PDO對象,連接數(shù)據(jù)庫
$pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root');

//2.創(chuàng)建預(yù)處理對象STMT
//更新數(shù)據(jù)
$sql = "UPDATE `user` SET `fenshu`=:fenshu,`create_time`=:create_time WHERE `id` = :id";

//3.驗(yàn)證SQL語句,創(chuàng)建預(yù)處理對象
$stmt = $pdo->prepare($sql);

//4.參數(shù)綁定
$id = 2;
$fenshu = 100;
$createTime = time();

$stmt->bindParam(':id',$id,PDO::PARAM_STR,20);
$stmt->bindParam(':fenshu',$fenshu,PDO::PARAM_INT);
$stmt->bindParam(':create_time',$createTime,PDO::PARAM_INT);

//5.執(zhí)行更新
if($stmt->execute()){
    echo($stmt->rowCount()>0) ? '成功更新了'.$stmt->rowCount().'條記錄' : '沒有記錄被更新';
} else {
    exit(print_r($stmt->errorInfo(),true));
}

封信.jpg

4、刪除數(shù)據(jù)

<?php
//1.創(chuàng)建PDO對象,連接數(shù)據(jù)庫
$pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root');

//2.創(chuàng)建預(yù)處理對象STMT
/*  //添加數(shù)據(jù)
$sql = "INSERT INTO `user` (`name`,`sex`,`age`,`fenshu`,`create_time`) VALUES (:name,:sex,:age,:fenshu,:create_time)";*/
//更新數(shù)據(jù)
$sql = "DELETE FROM `user` WHERE  `id` = :id";

//3.驗(yàn)證SQL語句,創(chuàng)建預(yù)處理對象
$stmt = $pdo->prepare($sql);

//4.參數(shù)綁定
$id = 2;

$stmt->bindParam(':id',$id,PDO::PARAM_STR,20);

//5.執(zhí)行刪除
if($stmt->execute()){
    echo($stmt->rowCount()>0) ? '成功刪除了'.$stmt->rowCount().'條記錄' : '沒有記錄被刪除';
} else {
    exit(print_r($stmt->errorInfo(),true));
}

刪除.jpg

5、查詢信息

<?php
//1.創(chuàng)建PDO對象,連接數(shù)據(jù)庫
$pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root');

//2.驗(yàn)證SQL語句,創(chuàng)建預(yù)處理對象
   //查詢指定字段和條件
//$sql = "SELECT `id`,`name`,`sex`,`age`,`fenshu`,`create_time` FROM `user` WHERE `fenshu`> 90";
   //查詢表中所有信息
$sql = "SELECT * FROM `user`";
$stmt = $pdo->prepare($sql);

//3.執(zhí)行
$stmt->execute();

//4.遍歷結(jié)果
while  ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $rows[] = $row;
}

?>

<style>
    table,th,td{
        border:1px solid #ccc;
    }

    table{
        text-align:center ;
        border:1px solid #666;
        width:50%;
        margin:30px auto;
        border-collapse: collapse;

    }
    table caption{
        font-size:18px;
        color:#333333;
        font-weight:bolder;
        margin-bottom: 15px;
    }

    table tr:first-child{
        font-size:16px;
        color:white;
        background-color: deeppink;
    }

</style>
<table>
    <caption>成  績  表</caption>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>性別</th>
        <th>年齡</th>
        <th>分?jǐn)?shù)</th>
        <th>注冊時(shí)間</th>
    </tr>

    <?php foreach ($rows as $row) : ?>
        <tr>
            <td><?php echo $row['id'] ?></td>
            <td><?php echo $row['name'] ?></td>
            <td><?php echo $row['sex'] ?></td>
            <td><?php echo $row['age'] ?></td>
            <td><?php echo $row['fenshu'] ?></td>
            <td><?php echo date('Y/m/d',$row['create_time']) ?></td>
        </tr>
    <?php endforeach; ?>
</table>

QQ截圖20190208180136.jpg

PDO操作很重要,還有很多視頻中沒有提到的參數(shù),要多練習(xí)

批改老師:韋小寶批改時(shí)間:2019-02-11 09:50:10
老師總結(jié):嗯!不錯不錯!寫的很棒!pdo創(chuàng)建數(shù)據(jù)表并且對表操作 寫的都很好!繼續(xù)加油吧!

發(fā)佈手記

熱門詞條