標籤:錯誤 bin 大數 ini ble cal bool integer prepare
MySQLi預先處理涉及到以下幾個函數:
mysqli_stmt mysqli_prepare ( mysqli $link , string $query )bool mysqli_stmt_bind_param ( mysqli_stmt $stmt , string $types , mixed &$var1 [, mixed &$... ] )bool mysqli_stmt_execute ( mysqli_stmt $stmt )bool mysqli_stmt_close ( mysqli_stmt $stmt )mixed mysqli_query ( mysqli $link , string $query)bool mysqli_set_charset ( mysqli $link , string $charset )
具體的範例程式碼如下:
<?php $conn=mysqli_connect("localhost","root","root","test");//設定字元集mysqli_set_charset($conn,"utf8");//預先處理,注意返回的類型是mysqli_stmt$stmt=mysqli_prepare($conn,"insert into aaa values (?,?)");// 綁定參數,第二個參數,請看下文註解// 注意,在$id,$name等變數的位置,不能出現常亮// mysqli_stmt_bind_param($stmt,"is",5,‘aaaaa‘);寫法是錯誤的mysqli_stmt_bind_param($stmt,"is",$id,$name);// 參數賦值$id="99";$name="xxxxxx";//執行預先處理mysqli_stmt_execute($stmt);//關閉預先處理,//注意關閉的不是資料庫連接,而是“預先處理”mysqli_stmt_close($stmt);//關閉資料庫連接mysqli_close($conn); ?>
這裡注意綁定參數的時候,
bool mysqli_stmt_bind_param ( mysqli_stmt $stmt , string $types , mixed &$var1 [, mixed &$... ] )
第二個參數是類型(type),類型有4個類型,分別是:
i(整型integer)、
d(雙精確度浮點數double)、
s(字串string)、
b(二進位大資料區塊blob)
每一個類型,即每個字母(i/d/s/b)對應一個參數,每個參數的位置所對應的類型要正確,順序別顛倒
MySQLi面向過程實踐---預先處理