PHP mysqli stmt_init() 函数
示例 - 面向对象的样式
初始化声明并返回 stmt_prepare() 使用的对象:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
$city="Sandnes";
// Create a prepared statement
$stmt = $mysqli -> stmt_init();
if ($stmt -> prepare("SELECT District FROM City WHERE Name=?")) {
// Bind parameters
$stmt -> bind_param("s", $city);
// Execute query
$stmt -> execute();
// Bind result variables
$stmt -> bind_result($district);
// Fetch value
$stmt -> fetch();
printf("%s is in district %s", $city, $district);
// Close statement
$stmt -> close();
}
$mysqli -> close();
?>
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
$city="Sandnes";
// Create a prepared statement
$stmt = $mysqli -> stmt_init();
if ($stmt -> prepare("SELECT District FROM City WHERE Name=?")) {
// Bind parameters
$stmt -> bind_param("s", $city);
// Execute query
$stmt -> execute();
// Bind result variables
$stmt -> bind_result($district);
// Fetch value
$stmt -> fetch();
printf("%s is in district %s", $city, $district);
// Close statement
$stmt -> close();
}
$mysqli -> close();
?>
查看底部的程序样式示例。
定义和用法
stmt_init() / mysqli_stmt_init() 函数初始化声明并返回 mysqli_stmt_prepare() 使用的对象。
语法
面向对象的风格:
$mysqli -> stmt_init()
程序风格:
mysqli_stmt_init(connection)
参数值
参数 | 描述 |
---|---|
connection | 必需。规定要使用的 MySQL 连接。 |
技术细节
返回值: | 返回一个对象。 |
---|---|
PHP 版本: | 5+ |
示例 - 程序样式
初始化声明并返回 mysqli_stmt_prepare() 使用的对象:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
$city="Sandnes";
// Create a prepared statement
$stmt = mysqli_stmt_init($con);
if (mysqli_stmt_prepare($stmt, "SELECT District FROM City WHERE Name=?")) {
// Bind parameters
mysqli_stmt_bind_param($stmt, "s", $city);
// Execute query
mysqli_stmt_execute($stmt);
// Bind result variables
mysqli_stmt_bind_result($stmt, $district);
// Fetch value
mysqli_stmt_fetch($stmt);
printf("%s is in district %s", $city, $district);
// Close statement
mysqli_stmt_close($stmt);
}
mysqli_close($con);
?>
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
$city="Sandnes";
// Create a prepared statement
$stmt = mysqli_stmt_init($con);
if (mysqli_stmt_prepare($stmt, "SELECT District FROM City WHERE Name=?")) {
// Bind parameters
mysqli_stmt_bind_param($stmt, "s", $city);
// Execute query
mysqli_stmt_execute($stmt);
// Bind result variables
mysqli_stmt_bind_result($stmt, $district);
// Fetch value
mysqli_stmt_fetch($stmt);
printf("%s is in district %s", $city, $district);
// Close statement
mysqli_stmt_close($stmt);
}
mysqli_close($con);
?>