首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

mysqli_stmt::execute

(PHP 5, PHP 7)

mysqli_stmt :: execute - mysqli_stmt_execute - 执行准备好的查询

描述

面向对象的风格

代码语言:javascript
复制
bool mysqli_stmt::execute ( void )

程序风格

代码语言:javascript
复制
bool mysqli_stmt_execute ( mysqli_stmt $stmt )

执行先前使用mysqli_prepare()函数准备的查询。执行时,任何存在的参数标记都将自动替换为适当的数据。

如果语句是UPDATEDELETEINSERT,则可以使用mysqli_stmt_affected_rows()函数确定受影响的行的总数。同样,如果查询产生结果集,则使用mysqli_stmt_fetch()函数。

注意:使用mysqli_stmt_execute()时,必须使用mysqli_stmt_fetch()函数在执行任何其他查询之前获取数据。

参数

代码语言:txt
复制
`stmt`   

仅过程风格:由mysqli_stmt_init()返回的语句标识符。

返回值

返回TRUE成功或返回FALSE失败时返回。

例子

Example #1 Object oriented style

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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);
?>

上面的例子会输出:

代码语言:javascript
复制
Stuttgart (DEU,Baden-Wuerttemberg)
Bordeaux (FRA,Aquitaine)

扫码关注腾讯云开发者

领取腾讯云代金券