您好,欢迎访问一九零五行业门户网

PHP学习笔记之三 数据库基本操作_PHP教程

下面是在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)下面就来建立一个页面来完成新建用户的页面。首先是一个简单的表单:
复制代码 代码如下:
username
password
confirm password
php通过$_post数组来获得通过post方法提交的表单中的数据。在php程序中,我们首先要判断是有ok字段,从而判断出该页面是首次访问,还是用户点击ok后提交的,接着判断两次密码输入是否统一。然后就可以获取到用户名和密码,插入数据库中。php连接mysql数据库一般可以利用mysql扩展或者mysqli扩展,mysqli扩展比较新一点,这里我们采用这种方式。mysqli可能需要安装配置下,不过在我的环境中是默认装好的。利用mysqli扩展操作数据库一般分为如下几步:构造mysqli对象,构造statement,绑定参数,执行,关闭。代码如下:
复制代码 代码如下:
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。最后,再为这个页面添加一点提示信息:
复制代码 代码如下:
password and confirm password must match.
再接下来,我们编写一个用户列表页面。
复制代码 代码如下:
user namecreatetimeaction
query($query);
while($row=$res->fetch_array()) {
?>
= $row['username'] ?>
= date('y-m-d',strtotime($row['createtime']))?>
edit
delete
close();
$mysql->close();
?>
http://www.bkjia.com/phpjc/322815.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/322815.htmltecharticle下面是在linux上登录mysql,创建数据库和创建表的过程。 yin@yin-ubuntu10:~$ mysql -u root -p enter password: welcome to the mysql monitor. commands end with ; or \...
其它类似信息

推荐信息