mysqli_stmt_prepare 准备SQL statement以备执行

注意:

bind_param的参数貌似不能‘运算’,只能接受单独一个变量

 

此函数应该用于参数绑定过程中

该prepare语句中的parameter maker也就是那个参数绑定的的问号‘?’,在执行语句获取结果executing the statement or fetching rows之前必须用mysqli_stmt_bind_param()或 mysqli_stmt_bind_result() 进行绑定

写法:

面向对象:

mixed mysqli_stmt::prepare ( string $query )

面向过程:

bool mysqli_stmt_prepare ( mysqli_stmt $stmt , string $query )

示例:

[pcsh lang="php" tab_size="4" message="" hl_lines="" provider="manual"]

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* 检查数据连接是否成功 */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$city = "Amersfoort";

/* 创建准备语句 prepared statement */
$stmt =  $mysqli->stmt_init();
if ($stmt->prepare("SELECT District FROM City WHERE Name=?")) {

    /* 为占位符markers 绑定变量 */
    $stmt->bind_param("s", $city);

    /* 执行语句*/
    $stmt->execute();

    /* 把结果绑定到变量$district */
    $stmt->bind_result($district);

    /* 获取数据 */
    $stmt->fetch();

    printf("%s is in district %s\n", $city, $district);

    /* 关闭stmt*/
    $stmt->close();
}

/* 关闭数据库连接 */
$mysqli->close();
?>

[/pcsh]

此处评论已关闭