PHP 教程
初始化聲明并返回 mysqli_stmt_prepare() 使用的對(duì)象:
<?php // 假定數(shù)據(jù)庫(kù)用戶(hù)名:root,密碼:123456,數(shù)據(jù)庫(kù):JSON $con=mysqli_connect("localhost","root","123456","JSON"); if (mysqli_connect_errno($con)) { echo "連接 MySQL 失敗: " . mysqli_connect_error(); } // 修改數(shù)據(jù)庫(kù)連接字符集為 utf8 mysqli_set_charset($con,"utf8"); $country="CN"; // 創(chuàng)建預(yù)處理語(yǔ)句 $stmt=mysqli_stmt_init($con); if (mysqli_stmt_prepare($stmt,"SELECT name FROM websites WHERE country=?")) { ???? ????// 綁定參數(shù) ????mysqli_stmt_bind_param($stmt,"s",$country); ???? ????// 執(zhí)行查詢(xún) ????mysqli_stmt_execute($stmt); ???? ????// 綁定結(jié)果變量 ????mysqli_stmt_bind_result($stmt,$name); ???? ????// 獲取值 ????mysqli_stmt_fetch($stmt); ???? ????printf("%s 國(guó)家的網(wǎng)站為:%s",$country,$name); ???? ????// 關(guān)閉預(yù)處理語(yǔ)句 ????mysqli_stmt_close($stmt); } mysqli_close($con); ?>
mysqli_stmt_init() 函數(shù)初始化聲明并返回 mysqli_stmt_prepare() 使用的對(duì)象。
參數(shù) | 描述 |
---|---|
connection | 必需。規(guī)定要使用的 MySQL 連接。 |
返回值: | 返回一個(gè)對(duì)象。 |
---|---|
PHP 版本: | 5+ |