PHP基础Mysql扩展库

mysql扩展库操作步骤如下:

1.连接数据库
2.选择数据库
3.设置操作编码
4.发送指令sql,并返回结果集
    ddl:数据定义语句
    dml:数据操作语句
    dql:数据查询语句
    dtl:事务操作语句
5.处理返回结果
6.释放资源,关闭连接*/

   1: <?php
   2: header("Content-Type:text/html; charset=utf-8");
   3: /*在数据库创建一个表:
   4: creat table TestDB
   5: (
   6: Id int primary key auto_increment,
   7: uName varchar(50) not null,
   8: uPwd varchar(64) not null,
   9: uEmail varchar(128) not null,
  10: uAge unsigned tinyint not null
  11: )*/
  12:  
  13:  
  14: // 1.连接数据库
  15: $conn = mysql_connect("127.0.0.1","root","1234");
  16: if(!$conn){
  17:     die("连接失败:".mysql_error());
  18: }else {
  19:     echo "连接成功!";
  20: }
  21:  
  22: // 2.选择数据库
  23: mysql_select_db("test");
  24:  
  25:  
  26: // 4.发送指令sql,并返回结果集到内存中
  27: $sql= "select * from userinfo";
  28: //返回的结果类型为:mysql result
  29: $result = mysql_query($sql,$conn);//当已经连接后,第二个参数可以省略,一般不省略
  30:  
  31: echo "
";
  32:  
  33: // 5.处理返回的结果 
  34: // mysql_fetch_row()从结棍集中取出一行数据,将数据存入数组并返回
  35: while ($row = mysql_fetch_row($result)) {
  36:     //echo "$row[0]----$row[1]----$row[2]----$row[3]
";
  37:     foreach ($row as $value) {
  38:         echo "----".$value;
  39:     }
  40:     echo "
";
  41: }
  42:  
  43: // 6.释放资源,关闭连接
  44: mysql_free_result($result);//释放结果集在内存中占用的空间
  45: // mysql_close($conn);//关闭数据库连接,不需要手动关闭,脚本执行完成后会自动关闭,建议写
  46:  
  47:  
  48: ?>

 

使用mysql扩展库对数据进行DML增删改操作

   1: <?php
   2: //使用mysql扩展库对数据进行DML增删改操作
   3: //======================================
   4:  
   5: //创建连接
   6: $conn = @mysql_connect("localhost","root","1234");
   7: if(!$conn){
   8:     die("连接失败:".mysql_error());
   9: }
  10:  
  11: //选择数据库
  12: mysql_select_db("test2",$conn) or die("选择数据库失败:".mysql_error());
  13: //设置编码
  14: mysql_query("set names utf8");
  15:  
  16: // 增
  17: // $sql = "insert into userinfo(uName,uAge,uPwd) values('测试01',25,MD5('123'));";
  18: // 删
  19: // $sql = "delete from userinfo where id=9";
  20: // 改
  21: // $sql = "update userinfo set uAge=25 where Id=1";
  22:  
  23: //执行DML语句,返回true或false
  24: $result = mysql_query($sql,$conn);
  25:  
  26: if(!$result){
  27:     die("操作失败:".mysql_error());
  28: }
  29:  
  30: if(mysql_affected_rows($conn)>0){
  31:     echo "操作成功,".mysql_affected_rows($conn)."行受影响";
  32: }else{
  33:     echo "没有受影响的行数";
  34: }
  35:  
  36: ?>
原文地址:https://www.cnblogs.com/lt-style/p/3511508.html