PDO方法操作数据库

一、创建数据库

    create table user(

            id int unsigned auto_increment,

            username varchar(30) nor null,

            password varchar(30) nor null,

            primary key(id));

二、PDO方法的使用      

连接数据库
//$pdo=new PDO('mysql:host=localhost;dbname=test','root','123456');
var_dump($pdo);
//
//$sql='select * from user1';
//$smt=$pdo->query($sql);
返回关联数组
//$rows=$smt->fetchAll(PDO::FETCH_ASSOC );
返回索引数据
$rows=$smt->fetchAll(PDO::FETCH_NUM );
返回混合数组
$rows=$smt->fetchAll(PDO::FETCH_BOTH );
返回对象
$rows=$smt->fetchAll(PDO::FETCH_OBJ );
//
//echo '<pre>';
//print_r($rows);
//echo '</pre>';

三、通过PDO方法操作数据库

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="bs/css/bootstrap.min.css">
    <script src="bs/js/jquery.min.js"></script>
    <script src="bs/js/bootstrap.min.js"></script>
    <title>Document</title>
</head>
<body>
    <div class="container">
        <h1 class="page-header">查看用户</h1>
        <table class="table table-bordered table-striped">
            <tr>
                <th>编号</th>
                <th>用户名</th>
                <th>密码</th>
            </tr>
            <?php
            //连接数据库
            $pdo=new PDO('mysql:host=localhost;dbname=test','root','123456');
            //var_dump($pdo);

            $sql='select * from user1';
            $smt=$pdo->query($sql);
            //返回关联数组
            $rows=$smt->fetchAll(PDO::FETCH_ASSOC );
            print_r($rows);

            foreach ($rows as $row){
                echo "<tr>";
                echo "<td>{$row['id']}</td>";
                echo "<td>{$row['username']}</td>";
                echo "<td>{$row['password']}</td>";
                echo "</tr>";
            }
//            ?>
        </table>
    </div>
</body>
</html>

四、页面展示


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值