PHP mysqli_stmt_init() 函數
初始化聲明並返回 mysqli_stmt_prepare() 使用的對象:
<?php
// 假定資料庫用戶名:root,密碼:123456,資料庫:zaixian
$con=mysqli_connect("localhost","root","123456","zaixian");
if (mysqli_connect_errno($con))
{
echo "連接 MySQL 失敗: " . mysqli_connect_error();
}
// 修改資料庫連接字元集為 utf8
mysqli_set_charset($con,"utf8");
$country="CN";
// 創建預處理語句
$stmt=mysqli_stmt_init($con);
if (mysqli_stmt_prepare($stmt,"SELECT name FROM websites WHERE country=?"))
{
// 綁定參數
mysqli_stmt_bind_param($stmt,"s",$country);
// 執行查詢
mysqli_stmt_execute($stmt);
// 綁定結果變數
mysqli_stmt_bind_result($stmt,$name);
// 獲取值
mysqli_stmt_fetch($stmt);
printf("%s 國家的網站為:%s",$country,$name);
// 關閉預處理語句
mysqli_stmt_close($stmt);
}
mysqli_close($con);
?>
定義和用法
mysqli_stmt_init() 函數初始化聲明並返回 mysqli_stmt_prepare() 使用的對象。
語法
mysqli_stmt_init(connection);
| 參數 | 描述 |
|---|---|
| connection | 必需。規定要使用的 MySQL 連接。 |
技術細節
| 返回值: | 返回一個對象。 |
|---|---|
| PHP 版本: | 5+ |
PHP MySQLi 參考手冊
