1 min read

PHP:关于mysqli连续插入数据(预处理)

连接语句略.. 参考地址:PHP:mysqli面向过程查询小记

$sql = "INSERT INTO MyGuests VALUES(?, ?, ?)";

// 为 mysqli_stmt_prepare() 初始化 statement 对象
$stmt = mysqli_stmt_init($mysqli);

//预处理语句
if (mysqli_stmt_prepare($stmt, $sql)) {
    // 绑定参数
    mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);

    // 设置参数并执行
    $firstname = 'John';
    $lastname = 'Doe';
    $email = 'john@example.com';
    mysqli_stmt_execute($stmt);

    $firstname = 'Mary';
    $lastname = 'Moe';
    $email = 'mary@example.com';
    mysqli_stmt_execute($stmt);

    $firstname = 'Julie';
    $lastname = 'Dooley';
    $email = 'julie@example.com';
    mysqli_stmt_execute($stmt);
}