Blogger Information
Blog 48
fans 3
comment 1
visits 36982
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
利用PDO的预处理操作mysql数据库,进行简单的数据读取、更新及删除——2018年4月25日
JackBlog
Original
1110 people have browsed it


知识点:


PDO::prepare — 备要执行的SQL语句并返回一个 PDOStatement 对象

PDO::errorCode — 获取跟数据库句柄上一次操作相关的 SQLSTATE

PDO::errorInfo — 返回最后一次操作数据库的错误信息

PDOStatement::bindColumn — 绑定一列到一个 PHP 变量

PDOStatement::bindParam — 绑定一个参数到指定的变量名

PDOStatement::bindValue — 把一个值绑定到一个参数

PDOStatement::closeCursor — 关闭游标,使语句能再次被执行。

PDOStatement::columnCount — 返回结果集中的列数

PDOStatement::debugDumpParams — 打印一条 SQL 预处理命令

PDOStatement::errorCode — 获取跟上一次语句句柄操作相关的 SQLSTATE

PDOStatement::errorInfo — 获取跟上一次语句句柄操作相关的扩展错误信息

PDOStatement::execute — 执行一条预处理语句

PDOStatement::fetch — 从结果集中获取下一行

PDOStatement::fetchAll — 返回一个包含结果集中所有行的数组

PDOStatement::fetchColumn — 从结果集中的下一行返回单独的一列。

PDOStatement::fetchObject — 获取下一行并作为一个对象返回。

PDOStatement::getAttribute — 检索一个语句属性

PDOStatement::getColumnMeta — 返回结果集中一列的元数据

PDOStatement::nextRowset — 在一个多行集语句句柄中推进到下一个行集

PDOStatement::rowCount — 返回受上一个 SQL 语句影响的行数

PDOStatement::setAttribute — 设置一个语句属性

PDOStatement::setFetchMode — 为语句设置默认的获取模式。

GIF.gif


index.php实例

<!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">
    <title>Document</title>
    <style type="text/css">
        .table_user{
            margin: auto;
        }
        table{
            width: 1000px;
            margin: auto;
            text-align: center;
        }
        table td:first-child{
            width: 60px;
        }
        table tr td a{
            text-decoration: none;
            color: #ae1108;
        }
    </style>
</head>
<body>
<div class="table_user">
<table border="1" cellspacing="0" cellpadding="10">
    <caption><h2>会员信息表</h2></caption>
    <tr>
        <th>会员ID</th>
        <th>账号</th>
        <th>密码</th>
        <th>性别</th>
        <th>金币</th>
        <th>积分</th>
        <th>操作</th>
    </tr>
<?php

$pdo = new PDO('mysql:dbname=xy28','xy28','123456');
$sql = 'SELECT * FROM t_user where id>:id';
$stmt =$pdo->prepare($sql);

$data = ['id'=>0];
$stmt->execute($data);
if ($stmt->rowCount()>0) {
    $res = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($res as $val) {
        echo '<tr>';
        echo '<td>' . $val['id'] . '</td>';
        echo '<td>' . $val['username'] . '</td>';
        echo '<td>' . $val['password'] . '</td>';
        echo '<td>' . $val['sex'] . '</td>';
        echo '<td>' . $val['jb'] . '</td>';
        echo '<td>' . $val['jf'] . '</td>';
        echo '<td><a href="update.php?action=update&id='.$val['id'].'">重置密码</a>  <a href="delete.php?action=delete&id='.$val['id'].'">删除</a></td>';
        echo '</tr>';
    }
}
?>

</table>
</div>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


update.php实例

<?php
/*
 *PDO预处理用的是PDOStatement对象
 * pdo->prepare
 */
if ($_GET['action']=='update'){
    $id = $_GET['id'];
    $pass = rand(111111,999999);
    update($id,$pass);
}




function update($id,$pass){
    //连接数据库
    $pdo = new PDO('mysql:dbname=xy28','xy28','123456');
//准备sql
    $sql = 'UPDATE t_user SET password=sha1(:pass) where id=:id';
//创建预处理对象
    $stmt=$pdo->prepare($sql);
//绑定变量到预处理对象
    $data =['pass'=>$pass,'id'=>$id];
//执行sql
    if ($stmt->execute($data)){
//    成功返回受影响的记录数。

        echo "<script>alert(\"会员id:$id 密码重置成功,请记住新密码:$pass\")</script>";
        header("refresh:0;url=select.php");

    }
    $pdo=null;
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


delete.php实例

<?php
/*
 *PDO预处理用的是PDOStatement对象
 * pdo->prepare
 */
if ($_GET['action']=='delete'){
    $id = $_GET['id'];
    delete($id);
}



function delete($id){
    //连接数据库
    $pdo = new PDO('mysql:dbname=xy28','xy28','123456');
//准备sql
    $sql = 'delete from t_user  where id=:id';
//创建预处理对象
    $stmt=$pdo->prepare($sql);
//绑定变量到预处理对象
    $data =['id'=>$id];
//执行sql
    if ($stmt->execute($data)){
//    成功返回受影响的记录数。
        echo "<script>alert(\"会员id:$id 删除成功\")</script>";
        header("refresh:0;url=select.php");
    }
    $pdo=null;
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!