預處理語句對於防止 MySQL 注入是非常有用的。
PHP官方代碼示例:
面向對象風格
1 <?php 2 $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 3 4 /* check connection */ 5 if (mysqli_connect_errno()) { 6 printf("Connect failed: %s\n", mysqli_connect_error()); 7 exit(); 8 } 9 10 $city = "Amersfoort"; 11 12 /* create a prepared statement */ 13 if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) { 14 15 /* bind parameters for markers */ 16 $stmt->bind_param("s", $city); 17 18 /* execute query */ 19 $stmt->execute(); 20 21 /* bind result variables */ 22 $stmt->bind_result($district); 23 24 /* fetch value */ 25 $stmt->fetch(); 26 27 printf("%s is in district %s\n", $city, $district); 28 29 /* close statement */ 30 $stmt->close(); 31 } 32 33 /* close connection */ 34 $mysqli->close(); 35 ?>
面向過程風格
1 <?php 2 $link = mysqli_connect("localhost", "my_user", "my_password", "world"); 3 4 /* check connection */ 5 if (mysqli_connect_errno()) { 6 printf("Connect failed: %s\n", mysqli_connect_error()); 7 exit(); 8 } 9 10 $city = "Amersfoort"; 11 12 /* create a prepared statement */ 13 if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name=?")) { 14 15 /* bind parameters for markers */ 16 mysqli_stmt_bind_param($stmt, "s", $city); 17 18 /* execute query */ 19 mysqli_stmt_execute($stmt); 20 21 /* bind result variables */ 22 mysqli_stmt_bind_result($stmt, $district); 23 24 /* fetch value */ 25 mysqli_stmt_fetch($stmt); 26 27 printf("%s is in district %s\n", $city, $district); 28 29 /* close statement */ 30 mysqli_stmt_close($stmt); 31 } 32 33 /* close connection */ 34 mysqli_close($link); 35 ?>
參數有以下四種類型:
- i - integer(整型)
- d - double(雙精度浮點型)
- s - string(字符串)
- b - BLOB(binary large object:二進制大對象)
每個參數都需要指定類型。
以下為一些mysqli常用函數。
mysqli_fetch_field_direct() 以對象返回結果集中單字段的元數據。
mysqli_fetch_field() 以對象返回結果集中的下一個字段。
mysqli_fetch_fields() 返回代表結果集中字段的對象數組。
mysqli_fetch_lengths() 返回結果集中當前行的列長度。
mysqli_fetch_object() 以對象返回結果集的當前行。
mysqli_fetch_row() 從結果集中抓取一行並以枚舉數組的形式返回它。
mysqli_field_count() 返回最近一次查詢獲取到的列的數目。
mysqli_field_seek() 設置字段指針到特定的字段開始位置。
mysqli_field_tell() 返回字段指針的位置。
mysqli_free_result() 釋放與某個結果集相關的內存。
mysqli_get_charset() 返回字符集對象。
mysqli_get_client_info() 返回字符串類型的 Mysql 客戶端版本信息。
mysqli_get_client_stats() 返回每個客戶端進程的統計信息。
mysqli_get_client_version() 返回整型的 Mysql 客戶端版本信息。
mysqli_get_connection_stats() 返回客戶端連接的統計信息。
mysqli_get_host_info() 返回 MySQL 服務器主機名和連接類型。
mysqli_get_proto_info() 返回 MySQL 協議版本。
mysqli_get_server_info() 返回 MySQL 服務器版本。
mysqli_get_server_version() 返回整型的 MySQL 服務器版本信息。
mysqli_info() 返回最近一次執行的查詢的檢索信息。
mysqli_init() 初始化 mysqli 並且返回一個由 mysqli_real_connect() 使用的資源類型。
mysqli_insert_id() 返回最后一次查詢中使用的自動生成 id。
mysql_kill() 請求服務器終結某個 MySQL 線程。
mysqli_more_results() 檢查一個多語句查詢是否還有其他查詢結果集。
mysqli_multi_query() 在數據庫上執行一個或多個查詢。
mysqli_next_result() 從 mysqli_multi_query() 中准備下一個結果集。
mysqli_num_fields() 返回結果集中的字段數。
mysqli_num_rows() 返回結果集中的行數。
mysqli_options() 設置選項。
mysqli_ping() Ping 一個服務器連接,或者如果那個連接斷了嘗試重連。
mysqli_prepare() 准備一條用於執行的 SQL 語句。
mysqli_query() 在數據庫上執行查詢。
mysqli_real_connect() 打開一個到 Mysql 服務端的新連接。
mysqli_real_escape_string() 轉義在 SQL 語句中使用的字符串中的特殊字符。
mysqli_real_query() 執行 SQL 查詢。
mysqli_reap_async_query() 返回異步查詢的結果。
mysqli_refresh() 刷新表或緩存,或者重置復制服務器信息。
mysqli_rollback() 回滾當前事務。
mysqli_select_db() 改變連接的默認數據庫。
mysqli_set_charset() 設置默認客戶端字符集。
mysqli_set_local_infile_default() 清除用戶為 load local infile 命令定義的處理程序。
mysqli_set_local_infile_handler() 設置 LOAD DATA LOCAL INFILE 命令執行的回調函數。
mysqli_sqlstate() 返回前一個 Mysql 操作的 SQLSTATE 錯誤代碼。
mysqli_ssl_set() 使用 SSL 建立安裝連接。
mysqli_stat() 返回當前系統狀態。
mysqli_stmt_init() 初始化一條語句並返回一個由 mysqli_stmt_prepare() 使用的對象。
mysqli_store_result() 傳輸最后一個查詢的結果集。
mysqli_thread_id() 返回當前連接的線程 ID。
mysqli_thread_safe() 返回是否設定了線程安全。
mysqli_use_result() 初始化一個結果集的取回。
mysqli_warning_count() 返回連接中最后一次查詢的警告數量。