当前位置:Gxlcms > PHP教程 > PHP学习笔记之三数据库基本操作_PHP教程

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

时间:2021-07-01 10:21:17 帮助过:27人阅读

下面是在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)下面就来建立一个页面来完成新建用户的页面。首先是一个简单的表单:
代码如下:
  1. <br> <br> <br>PHP通过$_POST数组来获得通过post方法提交的表单中的数据。在PHP程序中,我们首先要判断是有OK字段,从而判断出该页面是首次访问,还是用户点击OK后提交的,接着判断两次密码输入是否统一。然后就可以获取到用户名和密码,插入数据库中。PHP连接MySQL数据库一般可以利用mysql扩展或者mysqli扩展,mysqli扩展比较新一点,这里我们采用这种方式。mysqli可能需要安装配置下,不过在我的环境中是默认装好的。利用mysqli扩展操作数据库一般分为如下几步:构造mysqli对象,构造statement,绑定参数,执行,关闭。代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><span style="CURSOR: pointer" onclick="doCopy('code69132')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->$match=true; <br>if(isset($_POST["ok"])) { <br>$pwd=$_POST["Password"]; <br>$pwdConfirm=$_POST["ConfirmPassword"]; <br>$match=($pwd==$pwdConfirm); <br>$conn=new mysqli("localhost","root","123","UseCase"); <br>if (mysqli_connect_errno()) { <br>printf("Connect failed: %s\n", mysqli_connect_error()); <br>exit(); <br>} <br>$query="insert into User(UserName,Password) values(?,?)"; <br>$stmt=$conn->stmt_init(); <br>$stmt->prepare($query); <br>$stmt->bind_param('ss',$name,$pwd); <br>$name=$_POST["UserName"]; <br>$pwd=$_POST["Password"]; <br>$stmt->execute(); <br>if($stmt->errno==0) { <br>$success=true; <br>}else { <br>$success=false; <br>} <br>$stmt->close(); <br>$conn->close(); <br>} <br>?> <br> <br>其中bind_param方法需要稍微解释下,第一个参数的含义是参数类型。每个字符对应一个参数,s表示字符串,i表示整数,d表示浮点数,b表示blob。最后,再为这个页面添加一点提示信息: <br><span style="CURSOR: pointer" onclick="doCopy('code47105')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->if(!$match) { ?> <br><p>Password and Confirm Password must match.</p> <br><!--?php <BR-->} <br>?> <br><!--?php <BR-->if(isset($success)) { <br>if($success) { <br>echo '<p>User Created Successfully!'; <br>}elseif($sucess==false) { <br>echo '</p><p>User Name existed.'; <br>} <br>} <br>?> <br> <br>再接下来,我们编写一个用户列表页面。 <br><span style="CURSOR: pointer" onclick="doCopy('code86626')"><u></u></span> 代码如下:</p><pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><br><br><br>include 'conn.php'; <br>$query="select * from User;"; <br>$res=$mysql->query($query); <br>while($row=$res->fetch_array()) { <br>?> <br><br><br><br><br><br>} <br>$res->close(); <br>$mysql->close(); <br>?> <br><table> <tbody><tr><th>User Name</th><th>CreateTime</th><th>Action</th> </tr> <!--?php <BR--><tr> <td><!--?= $row['UserName'] ?--></td> <td><!--?= date('Y-m-d',strtotime($row['CreateTime']))?--> </td> <td>">Edit <br>">Delete <br></td> </tr> <!--?php <BR--></tbody></table> <br></li><li><p></p></li><li><p align="left"><span id="url" itemprop="url">http://www.bkjia.com/PHPjc/322815.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/322815.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">下面是在Linux上登录mysql,创建数据库和创建表的过程。 yin@yin-Ubuntu10:~$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \...</span></p></li><li> </li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre>

人气教程排行