php整理(四): mysql

PHP学习(四)---PHP与数据库MySql

主要有以下的内容:

  1.怎么连接数据库

  2.怎么操作数据库

     (1)怎么执行sql语言

     (2)怎么处理返回的结果集

  方法一:面向过程(已经过时,只是了解) 

假设:  

1 $username=your_name;
2 $userpass=your_pass;
3 $dbhost=localhost;
4 $dbdatabase=your_database;
复制代码
 1 //生成一个连接
 2 $db_connect=mysql_connect($dbhost,$username,$userpass) or die("Unable to connect to the MySQL!");
 3 
 4 //选择一个需要操作的数据库
 5 mysql_select_db($dbdatabase,$db_connect);
 6 
 7 //执行MySQL语句
 8 $result=mysql_query("SELECT id,name FROM user");
 9 
10 //提取数据
11 $row=mysql_fetch_row($result);
复制代码

说明:

①在mysql_connect()、mysql_select_db()等函数之前使用@(错误控制运算符),可以忽略掉系统产生的错误信息,然后我们用die()来自定义错误信息;

②提取数据的时候,除了上面的mysql_fetch_row,常见的还有mysql_fetch_assoc和mysql_fetch_array,具体差别请查阅PHP Manual;

③对于mysql_query()函数的返回值,如果执行的语句有返回值(如SELECT、SHOW、DESCRIBE等),则返回相应数据(成功 时)或FALSE(失败时);如果执行的语句没有返回值(如DELETE、DROP、INSERT、UPDATE等),则返回TRUE(成功时)或 FALSE(失败时)。

   方法二:面向对象(连接MySql数据库必选)

复制代码
 1 //创建数据库对象
 2 $db=new MySQLi("localhost","root","123","exam");
 3 //判断是不是执行成功
 4 !mysqli_connect_error() or die("连接失败!");
 5 //执行sql语句
 6 $result=$db->query("SELECT * FROM student");
 7 //处理结果集
 8 $arr=$result->fetch_row();
 9 //输出结果
10 var_dump($arr);
复制代码

 

 经典例题:

  这是例子都是嵌在html中的,当然不嵌在也是可以的

  1.数据库中的数据以表格的形式输出

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>以表格的形式显示数据库</title>
 6 </head>
 7 
 8 <body>
 9 <?php
10     //连接数据库
11     $db=new MySQLi("localhost","root","123","testa");
12     //判断是不是连接成功
13     ! mysqli_connect_error() or die("连接失败!");
14     //执行sql语句
15     $result=$db->query("SELECT * FROM info;");
16     //处理结果集
17     $arr=$result->fetch_all();//一个记录一个记录的取,以索引数组的形式显示
18     //$arr=$result->fetch_all();//取出全部的记录,并以二维数组的形式显示
19     //$arr=$result->fetch_assoc();//一个记录一个记录的取,以关联数组的形式显示
20     //可以在这里边用echo写,但是感觉代码多,而且还很乱
21 ?>
22 <!--创建表格-->
23     <table border="1" width="100%" cellpadding="0" cellspacing="0">
24         <tr>
25             <td>代号</td>
26             <td>姓名</td>
27             <td>性别</td>
28             <td>民族</td>
29             <td>生日</td>
30         </tr>
31         <?php
32             for($i=0;$i<count($arr);$i++){
33                 echo "<tr>";
34                 for($j=0;$j<count($arr[$i]);$j++){
35                     echo "<td>{$arr[$i][$j]}</td>";    
36                 };
37                 echo "</tr>";
38                 /*如果行数不多:
39                     echo "<tr><td>{$arr[$i][0]}</td><td>{$arr[$i][1]}</td><td>{$arr[$i][2]}</td><td>{$arr[$i][3]}</td><td>{$arr[$i][4]}</td></tr>";
40                 */
41             }
42         ?>
45     </table>
46 </body>
47 </html>
复制代码

 

结果预览:

  

  2.数据库的数据以下拉列表的形式呈现

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>以下拉列表的形式展现</title>
 6 </head>
 7 <body>
 8 <!--连接数据库,以数组的形式显示数据-->
 9 <?php
10     //连接数据库和处理结果集
11     $db=new MySQLi("localhost","root","123","testa");
12     !mysqli_connect_error() or die("连接失败!");
13     $result=$db->query("SELECT * FROM nation;");
14     $arr=$result->fetch_all();
15 ?>
16 <select>
17     <?php
18         //对于数据比较的多的情况可以这样写,要是比较少的话,直接写也是很快的
19         for($i=0;$i<count($arr);$i++){
20                echo "<option value='{$arr[$i][0]}'>{$arr[$i][1]}</option>";
21         }
22         /*
23             <option>$arr[0][1]</option>......
24             表单元素中的value和name都是很重要的,一定要注意对其的使用
25         */
26     ?>    
27 </select>
28 </body>
29 </html>
复制代码

 

结果:

这是页面效果

这是代码显示的形式

总结:

    总结一下上边的这两个例子:

    (1)首先这两个例子都是用PHP来控制输出的(仔细体会这句话),即把数据库中的内容呈现个用户,当然这里边是可以加一些样式的,给用户一个很好的用户体验.在设计的时候,要把数据和HTML分开来考虑,之后在找到他们之间的接口,将二者融合在一起. PHP从数据库中提取数据,借助HTML将数据更好的展现给用户.比如,提取出来的数据一般是数组,用户是看不懂数组的,要用HTML表格的形式展现给用户,这就是分开考虑和融合二者.

    (2)注意细节,不管是用户体验的细节还是编程的细节.细节决定成败 

  3.利用php与数据库实现登录

    实现登录就是多了一步填写的用户信息和数据库中的用户信息做比较.(这里边要是结合JS和CSS能实现很好的用户体验效果)

   登录界面:

    

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>登录</title>
 6 <style>
 7     .login{
 8         width:50%;
 9         margin:0px auto;
10         text-align:center;    
11     }
12     table{
13         text-align:center;
14     }
15     
16 </style>
17 </head>
18 <body>
19 <div class="login">
20     <!--设置好了,登录的界面-->
21     <form action="loginTest.php" method="post">
22     <table width="50%" border="0" cellpadding="0" cellspacing="0" align="center">
23         <tr>
24             <td width="10%">用户名:</td>
25             <td align="left"><input type="text" name="name" style="width:100%;" /><br/></td>
26         </tr>
27         <tr>
28             <td width="20%">密码:</td>
29             <td align="left"><input type="password" name="password" style="width:100%;"/><br/></td>
30         </tr>
31         <tr align="center"><td colspan=2><input type="submit" value="登录"/></td></tr>
32     </table>
33     </form>
34   
35 </div>
36 </body>
37 </html>
复制代码

 

  结果:

登录处理页面:  

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>登录验证</title>
 6 </head>
 7 <body>
 8 
 9   <?php
10         //实现登录
11         $name=$_POST["name"];
12         $password=$_POST["password"];
13         //连接数据库
14         $db=new MySQLi("localhost","root","123","testa");
15         !mysqli_connect_error() or die("连接失败!");
16         $result=$db->query("SELECT count(*) FROM login WHERE username='{$name}' AND password='{$password}';");
17         $arr=$result->fetch_all();
18         //验证
19             if($arr[0][0]==1){
20                 echo "<h1>登录成功!</h1>";
21             }else{
22                 echo "<h2>登录失败!</h2>";
23             }
24         
25     ?>
26 </body>
27 </html>
复制代码

 

  注意的地方:这个地方的验证是用匹配的数量来验证的,为1时唯一匹配,登录成功,当然这不是唯一的方法

  4.在用户界面实现数据的增删改查(改和查难点)

总页面:

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>增加</title>
 6 </head>
 7     <?php
 8         include("DBDA.php");
 9         $db=new DBDA();
10         $sql="SELECT * FROM info";
11         $arr=$db->Query($sql,1,"testa");
12     ?>
13     <table width="100%" border="1" cellpadding="0" cellspacing="0">
14         <tr><td>代号</td><td>姓名</td><td>性别</td><td>民族</td><td>生日</td><td>操作</td></tr>
15         <?php
16             for($i=0;$i<count($arr);$i++){
17                 echo "<tr><td>{$arr[$i][0]}</td><td>{$arr[$i][1]}</td><td>{$arr[$i][2]}</td><td>{$arr[$i][3]}</td><td>{$arr[$i][4]}</td><td><a href='delete.php?code={$arr[$i][0]}'>删除</a><a href='update.php'>修改</a></td></tr>";
18                 
19             }
20         ?>
21     </table>
22         <a href="add.php"><input type="button" value="添加数据"/></a>
23 </body>
24 </html>
复制代码

 

效果:

增加:

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>添加</title>
 6 </head>
 7 <body>
 8     <form action="addTest.php" method="post">
 9         代号:<input type="text" name="code"/><br/>
10         姓名:<input type="text" name="name"/><br/>
11         <!--选择的男女-->
12         性别:<input type="radio" name="sex" value="1" checked="checked"/>男
13             <input type="radio" name="sex" value="0"/>女<br/>
14            <!--下拉列表的民族-->
15         民族:
16         <select name="nation">
17             <?php
18                 include("DBDA.php");
19                 $db=new DBDA();
20                 $sql="SELECT * FROM nation";
21                 $arr=$db->Query($sql,1,"testa");
22                 for($i=0;$i<count($arr);$i++){
23                     echo "<option value='{$arr[$i][0]}'>{$arr[$i][1]}</option>";
24                 }
25             ?>
26             
27         </select><br/>
28            生日:<input type="text" name="birthday"/><br/>
29         <input type="submit" value="确认添加"/>
30     </form>
31 </body>
32 </html>
复制代码

 

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>添加处理界面</title>
 6 </head>
 7 <body>
 8 <?php
 9     $code=$_POST["code"];
10     $name=$_POST["name"];
11     $sex=$_POST["sex"];
12     $nation=$_POST["nation"];
13     $birthday=$_POST["birthday"];
14     include("DBDA.php");
15     $db=new DBDA();
16     $sql="INSERT INTO info VALUES ('{$code}','{$name}',{$sex},'{$nation}',{$birthday});";
17     $result=$db->Query($sql,0);
18     if($result){
19         header("location:table.php");    
20     }else{
21         echo "<script>alert('添加失败!');</script>";
22     }
23 ?>
24 </body>
25 </html>
复制代码

 

 删除:

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>删除</title>
 6 </head>
 7 <body>
 8 <?php
 9     $code=$_GET["code"];
10     include("DBDA.php");
11     $db=new DBDA();
12     $sql="DELETE FROM info WHERE code='{$code}'";
13     $result=$db->Query($sql,0);
14     if($result){
15         header("location:table.php");
16     }
17 ?>
18 </body>
19 </html>
复制代码

 

修改:

 

5.封装数据库的连接(主要是连接函数)

 封装的链接数据库的类:

复制代码
 1 <?php
 2 
 3 class DBDA
 4 {
 5     public $host = "localhost"; //服务器地址
 6     public $uid = "root"; //数据库的用户名
 7     public $pwd = "123"; //数据库的密码
 8     
 9     //执行SQL语句,返回相应结果的函数
10     //$sql是要执行的SQL语句
11     //$type是SQL语句的类型,0代表增删改,1代表查询
12     //$db代表要操作的数据库
13     public function Query($sql,$type=1,$db="testa")
14     {
15         //造连接对象
16         $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db);
17         
18         //判断连接是否成功
19         !mysqli_connect_error() or die("连接失败!");
20         
21         //执行SQL语句
22         $result = $conn->query($sql);
23         
24         //判断SQL语句类型
25         if($type==1)
26         {
27             //如果是查询语句返回结果集的二维数组
28             return $result->fetch_all();
29         }
30         else
31         {
32             //如果是其他语句,返回true或false
33             return $result;
34         }
35     }
36     
37 }
复制代码

 

分页类

 View Code

 主要是如何使用:

  该类中有比较重要的函数,也是主要使用的函数

    该类的构造函数和fpage()函数

    

posted on 2016-04-27 21:40  年少不上班  阅读(190)  评论(0编辑  收藏  举报

导航