PHP学习笔记之三:数据库基本操作

本文介绍最基本最实用的数据库操作。首先简单复习下MySQL的使用方法,并且建好一张表备用。MySQL在Linux系统上一般都是装好的,在win下安装也很简单,不多做介绍。下面是在Linux上登录mysql,创建数据库和创建表的过程。

yin@yin-Ubuntu10:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 360
Server version: 5.1.41-3ubuntu12.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database UseCase;
Query OK, 1 row affected (0.00 sec)

mysql> use UseCase;
Database changed

mysql> create table User(UserName varchar(20) primary key,Password varchar(20) not null,CreateTime timestamp default current_timestamp);
Query OK, 0 rows affected (0.01 sec)

下面就来建立一个页面来完成新建用户的页面。首先是一个简单的表单:

 <form action="db.php" method="post">
            <dl>
                <dt>UserName</dt><dd><input name="UserName" maxlength="20" type="text"/></dd>
                <dt>Password</dt><dd><input name="Password" maxlength="20" type="password"/></dd>
                <dt>Confirm Password</dt><dd><input name="ConfirmPassword" maxlength="20" type="password"/></dd>
            </dl>     
            <input type="submit" name="ok" value="ok"/>
 </form>
 

PHP通过$_POST数组来获得通过post方法提交的表单中的数据。在PHP程序中,我们首先要判断是有OK字段,从而判断出该页面是首次访问,还是用户点击OK后提交的,接着判断两次密码输入是否统一。然后就可以获取到用户名和密码,插入数据库中。PHP连接MySQL数据库一般可以利用mysql扩展或者mysqli扩展,mysqli扩展比较新一点,这里我们采用这种方式。mysqli可能需要安装配置下,不过在我的环境中是默认装好的。利用mysqli扩展操作数据库一般分为如下几步:构造mysqli对象,构造statement,绑定参数,执行,关闭。代码如下:

<?php
        $match=true;
        if(isset($_POST["ok"])) {
            $pwd=$_POST["Password"];
            $pwdConfirm=$_POST["ConfirmPassword"];
            $match=($pwd==$pwdConfirm);
            $conn=new mysqli("localhost","root","123","UseCase");
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }
            $query="insert into User(UserName,Password) values(?,?)";
            $stmt=$conn->stmt_init();
            $stmt->prepare($query);
            $stmt->bind_param('ss',$name,$pwd);
            $name=$_POST["UserName"];
            $pwd=$_POST["Password"];
            $stmt->execute();
            if($stmt->errno==0) {
                $success=true;
            }else {
                $success=false;
            }
            $stmt->close();
            $conn->close();
        }
?>

其中bind_param方法需要稍微解释下,第一个参数的含义是参数类型。每个字符对应一个参数,s表示字符串,i表示整数,d表示浮点数,b表示blob。最后,再为这个页面添加一点提示信息:

 <?php
            if(!$match) { ?>
            <p>Password and Confirm Password must match.</p>
                <?php
            }
            ?>
            <?php
            if(isset($success)) {
                if($success) {
                    echo '<p>User Created Successfully!';
                }elseif($sucess==false) {
                    echo '<p>User Name existed.';
                }
            }
?>
再接下来,我们编写一个用户列表页面。
 <table>
            <tr><th>User Name</th><th>CreateTime</th><th>Action</th>
            </tr>
            <?php
            include 'conn.php';
            $query="select * from User;";
            $res=$mysql->query($query);
            while($row=$res->fetch_array()) {
                ?>
            <tr>
                <td><?= $row['UserName'] ?></td>
                <td><?= date('Y-m-d',strtotime($row['CreateTime']))?> </td>
                <td><a href="UserEdit.php?action=update&ID=<?= $row['UserName'] ?>">Edit</a>
                    <a href="action=delete&ID=<?= $row['UserName'] ?>">Delete</a>
                </td>
            </tr>
                <?php
            }
            $res->close();
            $mysql->close();
            ?>
</table>
posted @ 2011-01-16 13:40  yinzixin  阅读(565)  评论(1编辑  收藏  举报