mysqli使用預處理技術進行資料庫查詢的方法

2021-07-31 23:56:08 字數 673 閱讀 8180

php5.6版本以上

這裡實現查詢所有 id>5 的 id,title,contents值:

<?php

$mysqli = new mysqli("localhost","root","123456","liuyan");

if(!$mysqli)

//建立乙個預定義的物件 ?佔位

$sql = "select id,title,contents from news where id>?";

$mysqli_stmt = $mysqli->prepare($sql);

$id=10;

//繫結引數

$mysqli_stmt->bind_param("i",$id);

//繫結結果集

$mysqli_stmt->bind_result($id,$title,$contents);

//執行

$mysqli_stmt->execute();

//取出繫結的結果集

while($mysqli_stmt->fetch())

//關閉結果集

$mysqli_stmt->free_result();

$mysqli_stmt->close();

$mysqli->close();

?>

mysqli 預處理語句

預處理語句用於執行多個相同的 sql 語句,並且執行效率更高。設定編碼格式 header content type text html charset utf 8 獲取資源識別符號 結果集 mysqli new mysqli localhost root test 判斷是否連線成功 if mysql...

預處理技術

預處理技術概述 確保標頭檔案多次包含仍能安全工作的常用技術是預處理器,它由c 語言從c語言繼承而來。預處理器是在編譯之前執行的一段程式,可以部分地改變我們所寫的程式。之前已經用到了一項預處理功能 include,當預處理器看到 include 標記時就會用指定的標頭檔案的內容代替 include。c...

PHP 對MySQLI預處理的包裝

mysql 類 class mysql return this link 單例模式 public static function instance else 預處理sql private function prepare query else 值繫結 private function bind va...