PHP数据库MYSQL操作基础
PHP数据库MYSQL操作基础
1 数据库要求是UTF8格式,这样方便与汉字存取。
2 创建数据表
| CREATE TABLE user_info ( user_id varchar(15), user_name varchar(15), user_age int ) | 
3 数据库连接
connection.php 代码如下
| <?php //获取连接 $con = mysql_connect("localhost","root",""); if(!$con){ die("没有找到连接".mysql_errno()); } //设置字符集 mysql_query("set names 'utf8'"); // 连接test数据库 mysql_select_db("test2",$con); echo "数据库连接正常"; ?> | 
注意:
mysql_connect(servername,username,password);
| 参数 | 描述 | 
| servername | 可选。规定要连接的服务器。默认是 "localhost:3306"。 | 
| username | 可选。规定登录所使用的用户名。默认值是拥有服务器进程的用户的名称。 | 
| password | 可选。规定登录所用的密码。默认是 ""。 | 
4 通过 mysql_query() 函数创建数据表
| 
 <?php echo "创建连接"; $url = "127.0.0.1"; $user = "root"; $password = ""; $con = mysql_connect($url,$user,$password); if(!$con){ die("连接失败".mysql_error()); } mysql_select_db("test1"); $sql = "CREATE TABLE user_info ( user_id varchar(15), user_name varchar(15), user_age int )"; mysql_query($sql,$con); mysql_close($con); ?> | 
mysql_query() 函数执行一条 MySQL 语句。
语法:
mysql_query(query,connection)
| 参数 | 描述 | 
| query | 必需。规定要发送的 SQL 查询。注释:查询字符串不应以分号结束。 | 
| connection | 可选。规定 SQL 连接标识符。如果未规定,则使用上一个打开的连接。 | 
5 执行完成数据库操作以后关闭数据库连接mysql_close
6 数据库查询显示代码如下
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div align="center"> <form action="" method="post"> <a href="userinfo_add.php">添加</a> <input type="submit" value="查询"/> </form> <table align="center" border="1" width="70%"> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>修改</th> </tr> <?php include 'connection.php'; $sql= "select * from user_info"; //执行SQL 得到结果集 $result = mysql_query($sql,$con); while($row=mysql_fetch_array($result)){ ?> <tr> <td><?php echo $row['user_id'] ?></td> <td><?php echo $row['user_name'] ?></td> <td><?php echo $row['user_age'] ?></td> <td><a href="userinfo_update.php?userId=<?php echo $row['user_id'] ?>">修改</a> <a href="delete.php?userId=<?php echo $row['user_id'] ?>">删除</a> </td> </tr> <?php } ?> </table> </div> </body> </html> | 
POST 方法发送的请求的。
请看例子:
下面是一个简单的HTML网页。
| <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html> | 
当用户填写此表单并点击提交按钮后,表单数据会发送到名为 "welcome.php" 的 PHP 文件供处理。表单数据是通过 HTTP POST 方法发送的。
| <html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html> | 
mysql_fetch_array 函数的主要作用是:
mysql_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有
返回根据从结果集取得的行生成的数组,如果没有更多行则返回 false。
语法:
| mysql_fetch_array(data,array_type) | |
| 参数 | 描述 | 
| data | 可选。规定要使用的数据指针。该数据指针是 mysql_query() 函数产生的结果。 | 
| array_type | 可选。规定返回哪种结果。可能的值: 
 | 
7 新增数据记录
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> 
 <body> <div align="center"> <form action="userinfo_insert.php" method="post"> <h3>用户信息添加</h3> 用户编号:<input type="text" name="userId"/> <br/> 用户姓名:<input type="text" name="userName"/><br/> 用户年龄:<input type="text" name="userAge"/><br/> <input type="submit" value="添加用户" /> </form> </div> </body> </html> | 
userinfo_insert.php 代码如下:
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <?php //通过post获取页面提交数据信息 $userId = $_POST['userId']; $userName = $_POST['userName']; $userAge = $_POST['userAge']; if($userId!=null){ echo "用户编号:".$userId."<br/>"; echo "用户姓名:".$userName."<br/>"; echo "用户年龄:".$userAge."<br/>"; include 'connection.php'; $sql = "insert into user_info (user_id,user_name,user_age) values('$userId','$userName','$userAge')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "添加一条记录"; //关闭连接 mysql_close($con); } ?> | 
8 修改数据记录
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <div align="center"> <?php include 'connection.php'; $sql = "select * from user_info where user_id='".$_GET['userId']."'"; echo "sql:".$sql; $result = mysql_query($sql,$con); if($row = mysql_fetch_array($result)){ ?> <form action="update.php" method="post"> <h3>用户信息添加</h3> 用户编号:<input type="text" name="userId" value="<?php echo $row['user_id']?>"/> <br/> 用户姓名:<input type="text" name="userName" value="<?php echo $row['user_name']?>"/><br/> 用户年龄:<input type="text" name="userAge" value="<?php echo $row['user_age']?>"/><br/> <input type="submit" value="修改用户" /> </form> <?php }?> </div> </body> </html> | 
update.php 代码如下:
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <script type="text/javascript"> <?php 
 //通过post获取页面提交数据信息 $userId = $_POST['userId']; $userName = $_POST['userName']; $userAge = $_POST['userAge']; $conn = mysql_connect("127.0.0.1","root",""); mysql_query("set names 'utf8'"); //连接数据库 mysql_select_db("test1",$conn); $sql = "update user_info set user_name='".$userName."',user_age=".$userAge." where user_id='".$userId."'"; mysql_query($sql,$conn);//执行SQL $mark = mysql_affected_rows();//返回影响行数 $url = "userinf_select.php"; if($mark>0){ echo "alert('修改成功')"; }else{ echo "alert('修改失败')"; } mysql_close($conn); ?> window.location= "userinf_select.php"; </script> </body> </html> | 
9 删除数据记录
delete.php 代码
| <?php $userId = $_GET['userId']; include 'connection.php'; $sql = "delete from user_info where user_id='".$userId."'"; mysql_query($sql,$con); $mark = mysql_affected_rows();//返回影响行数 if($mark>0){ echo "delete OK"; }else{ echo "delete ERROR"; } mysql_close($con); ?> | 
10 GET vs. POST
GET 和 POST 都创建数组(例如,array( key => value, key2 => value2, key3 => value3, ...))。此数组包含键/值对,其中的键是表单控件的名称,而值是来自用户的输入数据。
GET 和 POST 被视作 $_GET 和 $_POST。它们是超全局变量,这意味着对它们的访问无需考虑作用域 - 无需任何特殊代码,您能够从任何函数、类或文件访问它们。
$_GET 是通过 URL 参数传递到当前脚本的变量数组。
$_POST 是通过 HTTP POST 传递到当前脚本的变量数组。
| 何时使用 GET? 通过 GET 方法从表单发送的信息对任何人都是可见的(所有变量名和值都显示在 URL 中)。GET 对所发送信息的数量也有限制。限制在大于 2000 个字符。不过,由于变量显示在 URL 中,把页面添加到书签中也更为方便。GET 可用于发送非敏感的数据。 注释:绝不能使用 GET 来发送密码或其他敏感信息! | 
| 何时使用 POST? 通过 POST 方法从表单发送的信息对其他人是不可见的(所有名称/值会被嵌入 HTTP 请求的主体中),并且对所发送信息的数量也无限制。 此外 POST 支持高阶功能,比如在向服务器上传文件时进行 multi-part 二进制输入。 不过,由于变量未显示在 URL 中,也就无法将页面添加到书签。 提示:开发者偏爱 POST 来发送表单数据。 | 
 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号