mysqli_stmt::execute
mysqli_stmt::execute
mysqli_stmt_execute
(PHP 5, PHP 7)
mysqli_stmt :: execute - mysqli_stmt_execute - 执行准备好的查询
描述
面向对象的风格
bool mysqli_stmt::execute ( void )
程序风格
bool mysqli_stmt_execute ( mysqli_stmt $stmt )
执行先前使用mysqli_prepare()函数准备的查询。执行时,任何存在的参数标记都将自动替换为适当的数据。
如果语句是UPDATE
,DELETE
或INSERT
,则可以使用mysqli_stmt_affected_rows()函数确定受影响的行的总数。同样,如果查询产生结果集,则使用mysqli_stmt_fetch()函数。
注意
:使用mysqli_stmt_execute()时
,必须使用mysqli_stmt_fetch()函数在执行任何其他查询之前获取数据。
参数
`stmt`
仅过程风格:由mysqli_stmt_init()返回的语句标识符。
返回值
返回TRUE
成功或返回FALSE
失败时返回。
例子
Example #1 Object oriented style
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
$mysqli->query("CREATE TABLE myCity LIKE City"
/* Prepare an insert statement */
$query = "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)";
$stmt = $mysqli->prepare($query
$stmt->bind_param("sss", $val1, $val2, $val3
$val1 = 'Stuttgart';
$val2 = 'DEU';
$val3 = 'Baden-Wuerttemberg';
/* Execute the statement */
$stmt->execute(
$val1 = 'Bordeaux';
$val2 = 'FRA';
$val3 = 'Aquitaine';
/* Execute the statement */
$stmt->execute(
/* close statement */
$stmt->close(
/* retrieve all rows from myCity */
$query = "SELECT Name, CountryCode, District FROM myCity";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_row()) {
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]
}
/* free result set */
$result->close(
}
/* remove table */
$mysqli->query("DROP TABLE myCity"
/* close connection */
$mysqli->close(
?>
Example #2 Procedural style
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world"
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
mysqli_query($link, "CREATE TABLE myCity LIKE City"
/* Prepare an insert statement */
$query = "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)";
$stmt = mysqli_prepare($link, $query
mysqli_stmt_bind_param($stmt, "sss", $val1, $val2, $val3
$val1 = 'Stuttgart';
$val2 = 'DEU';
$val3 = 'Baden-Wuerttemberg';
/* Execute the statement */
mysqli_stmt_execute($stmt
$val1 = 'Bordeaux';
$val2 = 'FRA';
$val3 = 'Aquitaine';
/* Execute the statement */
mysqli_stmt_execute($stmt
/* close statement */
mysqli_stmt_close($stmt
/* retrieve all rows from myCity */
$query = "SELECT Name, CountryCode, District FROM myCity";
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]
}
/* free result set */
mysqli_free_result($result
}
/* remove table */
mysqli_query($link, "DROP TABLE myCity"
/* close connection */
mysqli_close($link
?>
上面的例子会输出:
Stuttgart (DEU,Baden-Wuerttemberg)
Bordeaux (FRA,Aquitaine)