国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PDO連接數(shù)據(jù),并顯示數(shù)據(jù)

Original 2018-11-26 11:03:00 338
abstract:PDO連接數(shù)據(jù)庫優(yōu)點(diǎn):    兼容多數(shù)據(jù)庫 模塊化,加載驅(qū)動快 輕型函數(shù),PHP實現(xiàn)了抽象和兼容PDO連接數(shù)據(jù)庫步驟:1、創(chuàng)建PDO對象    $dsn = 'mysql:host=127.0.0.1;dbname=php_edu;';    $username

PDO連接數(shù)據(jù)庫優(yōu)點(diǎn):

    兼容多數(shù)據(jù)庫 模塊化,加載驅(qū)動快 輕型函數(shù),PHP實現(xiàn)了抽象和兼容

PDO連接數(shù)據(jù)庫步驟:

1、創(chuàng)建PDO對象

    $dsn = 'mysql:host=127.0.0.1;dbname=php_edu;';

    $username = 'root';

    $pass = 'root';

$pdo = new PDO($dsn,$username,$pass);

2、執(zhí)行預(yù)處理方法,創(chuàng)建預(yù)處理對象

    $sql = 'SQL語句';

$stmt = $pdo->prepare($sql);

3、執(zhí)行查詢

    參數(shù)綁定:

        bindParam(':占位符',變量,類型常量)

        bindValue(':占位符',值或變量,類型常量)

$stmt->execute();

    列綁定:

        bindColumn('列名/索引',變量,變量類型,最大長度)

4、解析結(jié)果集

$stmt->fectchAll();

5、遍歷結(jié)果集:通常用foreach()結(jié)構(gòu)

6、釋放結(jié)果集

$stmt = null;

7、關(guān)閉連接

$pdo = null;

//1.創(chuàng)建PDO對象,連接數(shù)據(jù)庫
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php_edu;','root','root');
//2.創(chuàng)建預(yù)處理對象STMT
$sql = 'SELECT user_id,name,email,create_time FROM user WHERE  status = :status';
$stmt = $pdo->prepare($sql);
//參數(shù)綁定
$stmt->bindValue('status',1,PDO::FETCH_ASSOC);  #第二個參數(shù)支持變量、字面值
//3.執(zhí)行查詢
$stmt->execute();
//4.遍歷結(jié)果
//列綁定  四個變量四個綁定
$stmt->bindColumn(1,$id,PDO::PARAM_INT);
$stmt->bindColumn(2,$name,PDO::PARAM_STR,20);
$stmt->bindColumn(3,$email,PDO::PARAM_STR,100);
$stmt->bindColumn(4,$createTime,PDO::PARAM_STR,100);
$rows = [];
while ($stmt->fetch(PDO::FETCH_BOUND)){
//    將變量轉(zhuǎn)為數(shù)組 compact 參數(shù)是去掉 $ 的變量名字符串
    $rows[] = compact('id','name','email','createTime');
}
//5.釋放結(jié)果集
$stmt = null;
//6、關(guān)閉連接
$pdo = null;
?>
<style>
    table,th,td{border: 1px solid #333;font-size: 16px;}
    table{text-align: center;border-collapse: collapse;width: 50%;margin: 30px auto;}
    table caption{font-size: 20px;font-weight: bold;margin-bottom: 15px;}
    table tr:first-child{background: #7ccfd2;}
</style>
<table>
    <caption>員工表</caption>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>郵箱</th>
        <th>創(chuàng)建時間</th>
    </tr>
    <?php foreach ($rows as $row) : ?>
    <tr>
        <td><?php echo $row['id'] ?></td>
        <td><?php echo $row['name'] ?></td>
        <td><?php echo $row['email'] ?></td>
        <td><?php echo $row['createTime'] ?></td>
    </tr>
    <?php endforeach; ?>

</table>


Correcting teacher:韋小寶Correction time:2018-11-26 11:14:01
Teacher's summary:寫的很不錯!很完整!pdo鏈接數(shù)據(jù)庫是很重要的一部分!課后還得多多練習(xí)!

Release Notes

Popular Entries