加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-24 20:28:37 所属栏目:PHP 来源:互联网
导读:本文介绍最基本最实用的数据库操作。首先简单复习下MySQL的使用方法,并且建好一张表备用。MySQL在Linux系统上一般都是装好的,在win下安装也很简单,不多做介绍。

</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 href="action=delete&ID=<?= $row['UserName'] ?>">Delete
</td>
</tr>
<?php
}
$res->close();
$mysql->close();
?>
</table>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

下面是在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)下面就来建立一个页面来完成新建用户的页面。首先是一个简单的表单:
<div class="codetitle"><a style="CURSOR: pointer" data="24090" class="copybut" id="copybut24090" onclick="doCopy('code24090')"> 代码如下:<div class="codebody" id="code24090">
<form action="db.php" method="post">


UserName
<input name="UserName" maxlength="20" type="text"/>

Password
<input name="Password" maxlength="20" type="password"/>

Confirm Password
<input name="ConfirmPassword" maxlength="20" type="password"/>


<input type="submit" name="ok" value="ok"/>


PHP通过$_POST数组来获得通过post方法提交的表单中的数据。在PHP程序中,我们首先要判断是有OK字段,从而判断出该页面是首次访问,还是用户点击OK后提交的,接着判断两次密码输入是否统一。然后就可以获取到用户名和密码,插入数据库中。PHP连接MySQL数据库一般可以利用mysql扩展或者mysqli扩展,mysqli扩展比较新一点,这里我们采用这种方式。mysqli可能需要安装配置下,不过在我的环境中是默认装好的。利用mysqli扩展操作数据库一般分为如下几步:构造mysqli对象,构造statement,绑定参数,执行,关闭。代码如下:
<div class="codetitle"><a style="CURSOR: pointer" data="15804" class="copybut" id="copybut15804" onclick="doCopy('code15804')"> 代码如下:<div class="codebody" id="code15804">
<?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: %sn",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。最后,再为这个页面添加一点提示信息:
<div class="codetitle"><a style="CURSOR: pointer" data="93777" class="copybut" id="copybut93777" onclick="doCopy('code93777')"> 代码如下:<div class="codebody" id="code93777">
<?php
if(!$match) { ?>

Password and Confirm Password must match.


<?php
}
?>
<?php
if(isset($success)) {
if($success) {
echo '

User Created Successfully!';
}elseif($sucess==false) {
echo '

User Name existed.';
}
}
?>

再接下来,我们编写一个用户列表页面。
<div class="codetitle"><a style="CURSOR: pointer" data="33298" class="copybut" id="copybut33298" onclick="doCopy('code33298')"> 代码如下:<div class="codebody" id="code33298">
<table>
<tr>

User NameCreateTimeAction
    推荐文章
      热点阅读