PHP基础--PDO的常用操作

<?php


//PDO连接Mysql数据库
$dbms = 'mysql';
$dbname = 'thinkphp';
$host = 'localhost';
$user = 'root';
$pwd = '123456';
$dsn = "$dbms:host=$host;dbname=$dbname";
try {
    $pdo = new PDO($dsn, $user, $pwd);
    echo 'PDO连接Mysql成功!' . '<br>';
    var_dump($pdo);
} catch (Exception $e) {
    echo $e->getMessage() . "<br>";
}


//利用fetch()方法进行查询
$dbms = 'mysql';
$dbname = 'thinkphp';
$host = 'localhost';
$user = 'root';
$pwd = '123456';
$dsn = "$dbms:host=$host;dbname=$dbname";
try {
    $pdo = new PDO($dsn, $user, $pwd);
    echo '数据库连接成功!' . '<br>';
    $sql = "SELECT * FROM think_user";
    $result = $pdo->prepare($sql);
    $result->execute(); ?>
    <table>
        <?php
        while ($res = $result->fetch(PDO::FETCH_ASSOC)) {
            ?>
            <tr>
                <td align="center"><?php echo $res['id']; ?></td>
                <td align="center"><?php echo $res['user']; ?></td>
                <td align="center"><?php echo $res['email']; ?></td>
                <td align="center"><?php echo $res['date']; ?></td>
                <td align="center"><a href="#">删除</a></td>
            </tr>
            <?php
        }
        ?>
    </table>
    <?php
} catch (Exception $e) {
    echo $e->getMessage() . "<br>";
}
?>


<?php
//利用fetchAll()方法进行查询
$dbtype = 'mysql';
$dbhost = 'localhost';
$dbname = 'thinkphp';
$user = 'root';
$pwd = '123456';
$dsn = "$dbtype:host=$dbhost;dbname=$dbname";
try {
    $pdo = new PDO($dsn, $user, $pwd);
    echo '数据库连接成功!';
    $sql = "SELECT * FROM think_user";
    $result = $pdo->prepare($sql);
    $result->execute();
    $res = $result->fetchAll(PDO::FETCH_ASSOC);
    ?>
    <table>
        <?php
        for ($i = 0; $i < count($res); $i++) {
            ?>
            <tr>
                <td><?php echo $res[$i]['id']; ?></td>
                <td><?php echo $res[$i]['user']; ?></td>
                <td><?php echo $res[$i]['email']; ?></td>
                <td><?php echo $res[$i]['date']; ?></td>
            </tr>
            <?php
        }
        ?>
    </table>
    <?php
} catch (Exception $e) {
    echo '操作错误:' . $e->getMessage() . '<br>';
}
?>


<?php
//fetchColumn()方法获取下一行指定列的指
$dbtype = 'mysql';
$dbhost = 'localhost';
$dbname = 'thinkphp';
$user = 'root';
$pwd = '123456';
$dsn = "$dbtype:host=$dbhost;dbname=$dbname";
try {
    $pdo = new PDO($dsn, $user, $pwd);
    echo '数据库连接成功!';
    $sql = "SELECT * FROM think_user";
    $result = $pdo->prepare($sql);
    $result->execute(); ?>
    <table>
        <?php
        while ($res = $result->fetchColumn(0)) {
            ?>
            <tr>
                <td><?php echo $res; ?></td>
            </tr>
            <?php
        }
        ?>
    </table>
    <?php
} catch (Exception $e) {
    echo '操作出错:' . $e->getMessage() . '<br>';
}
?>


<?php
//PDO中捕获SQL语句的错误
$dbtype = 'mysql';
$dbhost = 'localhost';
$dbname = 'thinkphp';
$user = 'root';
$pwd = '123456';
$dsn = "$dbtype:host=$dbhost;dbname=$dbname";
$pdo = new PDO($dsn, $user, $pwd);
echo '数据库连接成功!' . '<br>';
$sql = "SELECT * FROM think_use"; //think_use表不存在
$stmt = $pdo->prepare($sql);
$stmt->execute();
$code = $stmt->errorCode();
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
    print_r($res);
    echo '<br>';
}
var_dump($stmt->errorInfo());
?>

 

posted @ 2017-06-20 14:05  zhaoxingya  阅读(276)  评论(0编辑  收藏  举报