PHP的mysqli扩展

PHP的mysqli扩展(一共有mysqli ,mysqli_stmt ,mysqli_result三个类)
1.||--------------启用mysqli扩展模块---------------
extension=php_mysqli.dll;


2.||=================mysqli的使用步骤=====================================
   1).|-------------连接MySQL服务器-------------------
      方法一:与mysql用发一样,只不过是把mysql换成了mysqli
      方法二:面向对象的方法
        $mysqli=new mysqli("localhost","user","pass","dbname");//连接mysql数据库服务器
   2).|------------处理连接错误报告------------------------------------     
    if(mysqli_connent_errno()){
      echo "连接失败:".mysqli_connect_error();
    }
    
   3).|------------执行SQL语句---------------------
     【1】$sql=select * from stu;
          $mysqli->query($sql);//执行sql语句
          注意:@1.在执行【增/删/改】的时候,成功返回true 失败返回false 
                @2.在执行【查询】的时候,成功会返回一个mysqli_result对象
   
                   
   4).|------------结果的处理使用mysqli_result类----------------------------------
     【1】 【增/删/改】结果的处理
          (1)影响的行数【增/删/改】
             $mysqli->affected_rows
             
         (2) 新插入的ID值【增】
             $mysqli->insert_id 
     【2】【查】的结果的处理      
         查询的时候返回的是一个mysqli_result的对象,所以可以调用mysqli_result中的一些函数
         (1).创建结果集的方法
             《1》$result = $mysqli->query($sql);
                  $result=$mysqli->query($sql,MYSQL_USER_RESULT);//第二个参数提供一个值,
                  在处理的数据集合尺寸比较大或者不适合一次全部取回到客户端的时候
             《2》$mysqli->real_query($sql);
                  $mysqli->store_result();
          (2).从结果集中解析数据
              《1》$result->fetch_row();
                   while($row=$result->fetch_row()){
                       echo $row['name'];
                    }
              《2》$result->fetch_assoc()
                     该方法将以一个关联数组的形式返回一条结果记录     
              《3》$result->fetch_array()
                    该方法将以一个关联和索引数组的形式返回一条结果记录        
              《4》$result->fetch_object()
                     该方法将以一个对象的形式返回一条结果记录         
                     
                     
          (3).从结果集中获取数据列的信息
              $result->field_count;//统计数据表中的列的个数
              $result->field_seek(); // 改变指针当前列的偏移位置
              $result->current_field;//当前列的位置
              $result->fetch_field();//当前列的信息
              $result->close();关闭结果集
    5).|----------------------关闭与MySQL服务器的连接----------------------
           $mysqli->close();      
           
  3.||==========================多条 SQL语句的执行===================================       
          【1】.没有结果集的 (insert update delete)
           $sql="insert into stu values ('a','b','c');update stu set name='lisi' where id=10;delete stu where id>10";
           
           $mysqli->muti_query($sql);
           
           最后插入的的ID:$mysqli->insert_id;还有用
           影响的行数就不管用了:$mysqli->affected_rows
           
           【2】.有结果集的(select)
           $sql="select name gb2312;select CURRENT_USER();SELECT name from stu";
           解析结果集
           do{
           $result=$mysqli->store_result();//返回的result的对象
           //用$result->fetch_assoc()解析
            foreach ($row = $result->fetch_assoc()){
              输出语句
            }           
       } while($mysqli->next_result())下一个结果集
       
    4.||==========================预处理(使用mysqli_stmt)==============================
         1).|------预处理的过程-----------------
           【1】.获取预处理语句对象
                $sql="insert into stu values(?,?,?,?)";
                $stmt=$mysqli->prepare($sql);
           【2】.绑定参数
                $stmt = $stmt->bind_param('issd',$a,$b,$c);
                $a="整型";
                $b="字符串型";
                $c="字符串型";
                $d="浮点数值";          
                //issd 分别代表的是整型 浮点型 和 字符串型
           【3】.执行准备好的语句
                 $stmt->exectue();
                 
                 
           【4】.执行结果的处理
                 $stmt->store_result();//取回全部查询结果   
                 $stmt->num_rows()//输出查询的记录的行数
                 $stmt->bind_result($name,$result,$phone);//将查询的结果绑定到变量
                 while($stmt->fetch()){
                   遍历输出变量
                 } 
                 
                 举例:
                 <?php
                      /* Open a connection */
                      $mysqli = new mysqli("localhost", "root", "1234", "lamp36");


                      /* check connection */
                      if (mysqli_connect_errno()) {
                          printf("Connect failed: %s\n", mysqli_connect_error());
                          exit();
                      }


                      $query = "SELECT * FROM gd2 ORDER BY php LIMIT 20";
                      if ($stmt = $mysqli->prepare($query)) {


                          /* execute query */
                          $stmt->execute();


                          /* store result */
                          var_dump($stmt->store_result());


                          printf("Number of rows: %d.\n", $stmt->num_rows);
                         $stmt->bind_result($name,$result);//将查询的结果绑定到变量
                                       while($stmt->fetch()){
                                        echo $name."<br/>";
                                echo $result."<p/>";
                                       } 
                          /* free result */
                          $stmt->free_result();
                        
                          /* close statement */
                          $stmt->close();
                      }


                      /* close connection */
                      $mysqli->close();
                    ?> 
                结果:bool(true) Number of rows: 10. 
                        36
                        40


                        34
                        50


                        36
                        58


                        36
                        58


                        34
                        59


                        36
                        70


                        36
                        78


                        34
                        80


                        34
                        89


                        34
                        90
           【5】.资源的释放
                 $stmt->close();                        
                                   
   5.||=======================事物处理=========================================
      【1】数据库首先保证是InnoDB格式的
      【2】关闭自动提交
           $mysqli->autocommit(0);//0关闭自动提交,1打开自动提交
      【3】执行sql语句 
      //向a的值中的cash减去price
        $result=$mysqli->query("update account set cash=cash-$price where name='a'");
        if(!$result or $mysqli->affect_rows!=1){
          $success = FALSE;
        }   
       
      //向b中的cash加上price
      $result = $mysqli->query("update account set cash = caseh +$price where name='b'");
       if(!$result or $mysql->affect_rows!=1){
        $success = FALSE;
       } 
       【4】//判断语句是够成功执行,如果成功执行则提交事物,否则回滚事物
       if($success){
        $mysql->commit();//十五提交
        echo "转账成功";
       }else{
         $mysql->rollback();//回滚当前的事物;
       }
       
       $mysqli->autocommit(1);//开启事物 
       
     6.||=======================类中方法和属性的说明===================================
            
                ========================================
                Mysqli类
                ========================================
                构造函数
                  mysqli - 初始化mysqli对象,连接数据库,参数(主机名,账号,密码,库名)


                方法
                  *autocommit(bool) - turns on or off auto-commiting database modifications
                            设置mysqli的事务是否自动提交 参数(布尔值)
                  *commit() - 提交事务方法(提交sql操作)commits the current transaction
                  
                  *rollback() - 事务回滚方法(撤销sql操作)rolls back the current transaction
                        
                  change_user(string user, string password, string database ) 
                        -更改用户信息 changes the user of the specified database connection


                  character_set_name - 返回默认字符集设置 returns the default character set for the database connection


                  *close -  关闭数据库 closes a previously opened connection


                  connect - 获取一个数据库连接 opens a new connection to MySQL database server


                  debug - 执行调试操作performs debugging operations


                  dump_debug_info - 转存调试信息dumps debug information


                  get_client_info - 获取数据库连接客户端版本信息returns client version


                  get_host_info - 返回连接类型returns type of connection used


                  get_server_info - 返回服务器版本信息returns version of the MySQL server


                  get_server_version - 返回服务版本号returns version of the MySQL server


                  init - 初始化mysqli对象 initializes mysqli object


                  info - 检索最近执行sql的查询信息 retrieves information about the most recently executed query


                  kill - 请求服务器杀死一个mysql进程asks the server to kill a mysql thread


                  multi_query - 执行多条sql查询语句performs multiple queries


                  more_results - 从多查询中检查是否有更多的查询结果check if more results exist from currently executed multi-query


                  next_result - 对多条查询中,读取下一个结果reads next result from currently executed multi-query


                  options - 设置选项set options


                  ping - ping服务器连接或重新连接pings a server connection or reconnects if there is no connection


                  *prepare -准备一个sql语句的执行,返回mysqli_stmt对象 prepares a SQL query


                  *query -执行sql语句查询,若是查询则返回mysqli_result结果集对象 performs a query


                  real_connect - 试图打开一个数据库连接attempts to open a connection to MySQL database server


                  escape_string - 转义sql语句的特殊字符escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection


                  select_db - 选择默认数据库selects the default database


                  set_charset - 设置客户端连接字符集sets the default client character set


                  ssl_set - 使用安全连接sets ssl parameters


                  stat - 获取系统状态gets the current system status


                  stmt_init- 初始化一个声明,返回mysqli_stmt对象 initializes a statement for use with mysqli_stmt_prepare


                  store_result - 从最后查询中转让结果集transfers a resultset from last query


                  use_result - 不缓存查询 transfers an unbuffered resultset from last query


                  thread_safe - 线程是否安全returns whether thread safety is given or not


                【属性】
                ===================================================================
                *affected_rows -影响的行数 gets the number of affected rows in a previous MySQL operation


                client_info - 客户端信息returns the MySQL client version as a string


                client_version -客户端版本 returns the MySQL client version as an integer


                *errno - 错误号returns the error code for the most recent function call


                *error - 错误信息returns the error string for the most recent function call


                *field_count - 字段数量 returns the number of columns for the most recent query


                host_info - 主机信息 returns a string representing the type of connection used


                info - 最近执行查询信息 retrieves information about the most recently executed query


                *insert_id - 最后插入的自增id号 returns the auto generated id used in the last query


                protocol_version - 协议版本 returns the version of the MySQL protocol used


                sqlstate - 最后错误号returns a string containing the SQLSTATE error code for the last error


                thread_id - 线程id号 returns the thread ID for the current connection


                warning_count - 警告的次数 returns the number of warnings generated during execution of the previous SQL statement






                【mysqli_stmt】
                =====================================================
                Represents a prepared statement. 


                【方法】


                bind_param - 绑定参数 binds variables to a prepared statement


                bind_result - binds variables to a prepared statement for result storage


                close - closes a prepared statement


                data_seek - seeks to an arbitrary row in a statement result set


                execute - 执行sql语句 executes a prepared statement


                fetch - fetches result from a prepared statement into bound variables


                free_result - frees stored result memory for the given statement handle


                result_metadata - retrieves a resultset from a prepared statement for metadata information


                prepare - prepares a SQL query


                send_long_data - sends data in chunks


                reset - resets a prepared statement


                store_result - buffers complete resultset from a prepared statement


                【属性】


                affected_rows - returns affected rows from last statement execution


                errno - returns errorcode for last statement function


                errno - returns errormessage for last statement function


                param_count - returns number of parameter for a given prepare statement


                sqlstate - returns a string containing the SQLSTATE error code for the last statement function




                ======================================================
                mysqli_result 结果集对象
                =======================================================
                Represents the result set obtained from a query against the database. 


                【方法】


                close - closes resultset


                data_seek - moves internal result pointer


                fetch_field - gets column information from a resultset


                fetch_fields - gets information for all columns from a resulset


                fetch_field_direct - gets column information for specified column


                fetch_array - 关联数组和索引式数组解析一条结果集fetches a result row as an associative array, a numeric array, or both.


                fetch_assoc - 以关联数组方式解析一条结果集 fetches a result row as an associative array


                fetch_object - 以对象方式解析一条结果集 fetches a result row as an object


                fetch_row - 以索引式数组解析结果集 gets a result row as an enumerated array


                close - frees result memory


                field_seek - set result pointer to a specified field offset


                【属性】


                current_field - returns offset of current fieldpointer


                field_count - returns number of fields in resultset


                lengths - returns an array of columnlengths


                num_rows - returns number of rows in resultset


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值