?php
// 假定數據庫用戶名:root,密碼:123456,數據庫:codingdict
$con=mysqli_connect("localhost","root","123456","codingdict");
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);
?>