PHP的mysqli擴(kuò)展(一共有mysqli ,mysqli_stmt ,mysqli_result三個類)
1.||--------------啟用mysqli擴(kuò)展模塊---------------
extension=php_mysqli.dll;
2.||=================mysqli的使用步驟=====================================
? ?1).|-------------連接MySQL服務(wù)器-------------------
? ? ? 方法一:與mysql用發(fā)一樣,只不過是把mysql換成了mysqli
? ? ? 方法二:面向?qū)ο蟮姆椒?br />? ? ? ? $mysqli=new mysqli("localhost","user","pass","dbname");//連接mysql數(shù)據(jù)庫服務(wù)器
? ?2).|------------處理連接錯誤報告------------------------------------ ? ??
? ? if(mysqli_connent_errno()){
? ? ? echo "連接失敗:".mysqli_connect_error();
? ? }
? ??
? ?3).|------------執(zhí)行SQL語句---------------------
? ? ?【1】$sql=select * from stu;
? ? ? ? ? $mysqli->query($sql);//執(zhí)行sql語句
? ? ? ? ? 注意:@1.在執(zhí)行【增/刪/改】的時候,成功返回true 失敗返回false?
? ? ? ? ? ? ? ? @2.在執(zhí)行【查詢】的時候,成功會返回一個mysqli_result對象
? ?
? ? ? ? ? ? ? ? ? ?
? ?4).|------------結(jié)果的處理使用mysqli_result類----------------------------------
? ? ?【1】 【增/刪/改】結(jié)果的處理
? ? ? ? ? (1)影響的行數(shù)【增/刪/改】
? ? ? ? ? ? ?$mysqli->affected_rows
? ? ? ? ? ? ?
? ? ? ? ?(2) 新插入的ID值【增】
? ? ? ? ? ? ?$mysqli->insert_id?
? ? ?【2】【查】的結(jié)果的處理 ? ? ?
? ? ? ? ?查詢的時候返回的是一個mysqli_result的對象,所以可以調(diào)用mysqli_result中的一些函數(shù)
? ? ? ? ?(1).創(chuàng)建結(jié)果集的方法
? ? ? ? ? ? ?《1》$result = $mysqli->query($sql);
? ? ? ? ? ? ? ? ? $result=$mysqli->query($sql,MYSQL_USER_RESULT);//第二個參數(shù)提供一個值,
? ? ? ? ? ? ? ? ? 在處理的數(shù)據(jù)集合尺寸比較大或者不適合一次全部取回到客戶端的時候
? ? ? ? ? ? ?《2》$mysqli->real_query($sql);
? ? ? ? ? ? ? ? ? $mysqli->store_result();
? ? ? ? ? (2).從結(jié)果集中解析數(shù)據(jù)
? ? ? ? ? ? ? 《1》$result->fetch_row();
? ? ? ? ? ? ? ? ? ?while($row=$result->fetch_row()){
? ? ? ? ? ? ? ? ? ? ? ?echo $row['name'];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? 《2》$result->fetch_assoc()
? ? ? ? ? ? ? ? ? ? ?該方法將以一個關(guān)聯(lián)數(shù)組的形式返回一條結(jié)果記錄 ? ??
? ? ? ? ? ? ? 《3》$result->fetch_array()
? ? ? ? ? ? ? ? ? ? 該方法將以一個關(guān)聯(lián)和索引數(shù)組的形式返回一條結(jié)果記錄 ? ? ? ?
? ? ? ? ? ? ? 《4》$result->fetch_object()
? ? ? ? ? ? ? ? ? ? ?該方法將以一個對象的形式返回一條結(jié)果記錄 ? ? ? ??
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? (3).從結(jié)果集中獲取數(shù)據(jù)列的信息
? ? ? ? ? ? ? $result->field_count;//統(tǒng)計數(shù)據(jù)表中的列的個數(shù)
? ? ? ? ? ? ? $result->field_seek(); // 改變指針當(dāng)前列的偏移位置
? ? ? ? ? ? ? $result->current_field;//當(dāng)前列的位置
? ? ? ? ? ? ? $result->fetch_field();//當(dāng)前列的信息
? ? ? ? ? ? ? $result->close();關(guān)閉結(jié)果集
? ? 5).|----------------------關(guān)閉與MySQL服務(wù)器的連接----------------------
? ? ? ? ? ?$mysqli->close(); ? ? ?
? ? ? ? ? ?
? 3.||==========================多條 SQL語句的執(zhí)行=================================== ? ? ??
? ? ? ? ? 【1】.沒有結(jié)果集的 (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;還有用
? ? ? ? ? ?影響的行數(shù)就不管用了:$mysqli->affected_rows
? ? ? ? ? ?
? ? ? ? ? ?【2】.有結(jié)果集的(select)
? ? ? ? ? ?$sql="select name gb2312;select CURRENT_USER();SELECT name from stu";
? ? ? ? ? ?解析結(jié)果集
? ? ? ? ? ?do{
? ? ? ? ? ?$result=$mysqli->store_result();//返回的result的對象
? ? ? ? ? ?//用$result->fetch_assoc()解析
? ? ? ? ? ? foreach ($row = $result->fetch_assoc()){
? ? ? ? ? ? ? 輸出語句
? ? ? ? ? ? } ? ? ? ? ??
? ? ? ?} while($mysqli->next_result())下一個結(jié)果集
? ? ? ?
? ? 4.||==========================預(yù)處理(使用mysqli_stmt)==============================
? ? ? ? ?1).|------預(yù)處理的過程-----------------
? ? ? ? ? ?【1】.獲取預(yù)處理語句對象
? ? ? ? ? ? ? ? $sql="insert into stu values(?,?,?,?)";
? ? ? ? ? ? ? ? $stmt=$mysqli->prepare($sql);
? ? ? ? ? ?【2】.綁定參數(shù)
? ? ? ? ? ? ? ? $stmt = $stmt->bind_param('issd',$a,$b,$c);
? ? ? ? ? ? ? ? $a="整型";
? ? ? ? ? ? ? ? $b="字符串型";
? ? ? ? ? ? ? ? $c="字符串型";
? ? ? ? ? ? ? ? $d="浮點(diǎn)數(shù)值"; ? ? ? ? ?
? ? ? ? ? ? ? ? //issd 分別代表的是整型 浮點(diǎn)型 和 字符串型
? ? ? ? ? ?【3】.執(zhí)行準(zhǔn)備好的語句
? ? ? ? ? ? ? ? ?$stmt->exectue();
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ?
? ? ? ? ? ?【4】.執(zhí)行結(jié)果的處理
? ? ? ? ? ? ? ? ?$stmt->store_result();//取回全部查詢結(jié)果 ??
? ? ? ? ? ? ? ? ?$stmt->num_rows()//輸出查詢的記錄的行數(shù)
? ? ? ? ? ? ? ? ?$stmt->bind_result($name,$result,$phone);//將查詢的結(jié)果綁定到變量
? ? ? ? ? ? ? ? ?while($stmt->fetch()){
? ? ? ? ? ? ? ? ? ?遍歷輸出變量
? ? ? ? ? ? ? ? ?}?
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ?舉例:
? ? ? ? ? ? ? ? ?<?php
? ? ? ? ? ? ? ? ? ? ? /* Open a connection */
? ? ? ? ? ? ? ? ? ? ? $mysqli = new mysqli("localhost", "root", "1234", "lamp36");
? ? ? ? ? ? ? ? ? ? ? /* check connection */
? ? ? ? ? ? ? ? ? ? ? if (mysqli_connect_errno()) {
? ? ? ? ? ? ? ? ? ? ? ? ? printf("Connect failed: %sn", 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);//將查詢的結(jié)果綁定到變量
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?while($stmt->fetch()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? echo $name."
";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? echo $result."
";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}?
? ? ? ? ? ? ? ? ? ? ? ? ? /* free result */
? ? ? ? ? ? ? ? ? ? ? ? ? $stmt->free_result();
? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? /* close statement */
? ? ? ? ? ? ? ? ? ? ? ? ? $stmt->close();
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? /* close connection */
? ? ? ? ? ? ? ? ? ? ? $mysqli->close();
? ? ? ? ? ? ? ? ? ? ?>?
? ? ? ? ? ? ? ? 結(jié)果: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】數(shù)據(jù)庫首先保證是InnoDB格式的
? ? ? 【2】關(guān)閉自動提交
? ? ? ? ? ?$mysqli->autocommit(0);//0關(guān)閉自動提交,1打開自動提交
? ? ? 【3】執(zhí)行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】//判斷語句是夠成功執(zhí)行,如果成功執(zhí)行則提交事物,否則回滾事物
? ? ? ?if($success){
? ? ? ? $mysql->commit();//十五提交
? ? ? ? echo "轉(zhuǎn)賬成功";
? ? ? ?}else{
? ? ? ? ?$mysql->rollback();//回滾當(dāng)前的事物;
? ? ? ?}
? ? ? ?
? ? ? ?$mysqli->autocommit(1);//開啟事物?
? ? ? ?
? ? ?6.||=======================類中方法和屬性的說明===================================
? ? ? ? ? ??
? ? ? ? ? ? ? ? ========================================
? ? ? ? ? ? ? ? Mysqli類
? ? ? ? ? ? ? ? ========================================
? ? ? ? ? ? ? ? 構(gòu)造函數(shù)
? ? ? ? ? ? ? ? ? mysqli - 初始化mysqli對象,連接數(shù)據(jù)庫,參數(shù)(主機(jī)名,賬號,密碼,庫名)
? ? ? ? ? ? ? ? 方法
? ? ? ? ? ? ? ? ? *autocommit(bool) - turns on or off auto-commiting database modifications
? ? ? ? ? ? ? ? ? ? ? ? ? ? 設(shè)置mysqli的事務(wù)是否自動提交 參數(shù)(布爾值)
? ? ? ? ? ? ? ? ? *commit() - 提交事務(wù)方法(提交sql操作)commits the current transaction
? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? *rollback() - 事務(wù)回滾方法(撤銷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 - 返回默認(rèn)字符集設(shè)置 returns the default character set for the database connection
? ? ? ? ? ? ? ? ? *close - ?關(guān)閉數(shù)據(jù)庫 closes a previously opened connection
? ? ? ? ? ? ? ? ? connect - 獲取一個數(shù)據(jù)庫連接 opens a new connection to MySQL database server
? ? ? ? ? ? ? ? ? debug - 執(zhí)行調(diào)試操作performs debugging operations
? ? ? ? ? ? ? ? ? dump_debug_info - 轉(zhuǎn)存調(diào)試信息dumps debug information
? ? ? ? ? ? ? ? ? get_client_info - 獲取數(shù)據(jù)庫連接客戶端版本信息returns client version
? ? ? ? ? ? ? ? ? get_host_info - 返回連接類型returns type of connection used
? ? ? ? ? ? ? ? ? get_server_info - 返回服務(wù)器版本信息returns version of the MySQL server
? ? ? ? ? ? ? ? ? get_server_version - 返回服務(wù)版本號returns version of the MySQL server
? ? ? ? ? ? ? ? ? init - 初始化mysqli對象 initializes mysqli object
? ? ? ? ? ? ? ? ? info - 檢索最近執(zhí)行sql的查詢信息 retrieves information about the most recently executed query
? ? ? ? ? ? ? ? ? kill - 請求服務(wù)器殺死一個mysql進(jìn)程asks the server to kill a mysql thread
? ? ? ? ? ? ? ? ? multi_query - 執(zhí)行多條sql查詢語句performs multiple queries
? ? ? ? ? ? ? ? ? more_results - 從多查詢中檢查是否有更多的查詢結(jié)果check if more results exist from currently executed multi-query
? ? ? ? ? ? ? ? ? next_result - 對多條查詢中,讀取下一個結(jié)果reads next result from currently executed multi-query
? ? ? ? ? ? ? ? ? options - 設(shè)置選項set options
? ? ? ? ? ? ? ? ? ping - ping服務(wù)器連接或重新連接pings a server connection or reconnects if there is no connection
? ? ? ? ? ? ? ? ? *prepare -準(zhǔn)備一個sql語句的執(zhí)行,返回mysqli_stmt對象 prepares a SQL query
? ? ? ? ? ? ? ? ? *query -執(zhí)行sql語句查詢,若是查詢則返回mysqli_result結(jié)果集對象 performs a query
? ? ? ? ? ? ? ? ? real_connect - 試圖打開一個數(shù)據(jù)庫連接attempts to open a connection to MySQL database server
? ? ? ? ? ? ? ? ? escape_string - 轉(zhuǎn)義sql語句的特殊字符escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection
? ? ? ? ? ? ? ? ? select_db - 選擇默認(rèn)數(shù)據(jù)庫selects the default database
? ? ? ? ? ? ? ? ? set_charset - 設(shè)置客戶端連接字符集sets the default client character set
? ? ? ? ? ? ? ? ? ssl_set - 使用安全連接sets ssl parameters
? ? ? ? ? ? ? ? ? stat - 獲取系統(tǒng)狀態(tài)gets the current system status
? ? ? ? ? ? ? ? ? stmt_init- 初始化一個聲明,返回mysqli_stmt對象 initializes a statement for use with mysqli_stmt_prepare
? ? ? ? ? ? ? ? ? store_result - 從最后查詢中轉(zhuǎn)讓結(jié)果集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 -影響的行數(shù) 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 - 字段數(shù)量 returns the number of columns for the most recent query
? ? ? ? ? ? ? ? host_info - 主機(jī)信息 returns a string representing the type of connection used
? ? ? ? ? ? ? ? info - 最近執(zhí)行查詢信息 retrieves information about the most recently executed query
? ? ? ? ? ? ? ? *insert_id - 最后插入的自增id號 returns the auto generated id used in the last query
? ? ? ? ? ? ? ? protocol_version - 協(xié)議版本 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 - 警告的次數(shù) returns the number of warnings generated during execution of the previous SQL statement
? ? ? ? ? ? ? ? 【mysqli_stmt】
? ? ? ? ? ? ? ? =====================================================
? ? ? ? ? ? ? ? Represents a prepared statement.?
? ? ? ? ? ? ? ? 【方法】
? ? ? ? ? ? ? ? bind_param - 綁定參數(shù) 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 - 執(zhí)行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 結(jié)果集對象
? ? ? ? ? ? ? ? =======================================================
? ? ? ? ? ? ? ? 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 - 關(guān)聯(lián)數(shù)組和索引式數(shù)組解析一條結(jié)果集fetches a result row as an associative array, a numeric array, or both.
? ? ? ? ? ? ? ? fetch_assoc - 以關(guān)聯(lián)數(shù)組方式解析一條結(jié)果集 fetches a result row as an associative array
? ? ? ? ? ? ? ? fetch_object - 以對象方式解析一條結(jié)果集 fetches a result row as an object
? ? ? ? ? ? ? ? fetch_row - 以索引式數(shù)組解析結(jié)果集 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





