实例 (MySQLi 使用预处理语句)

来源:互联网 发布:filco 蓝牙mac 编辑:程序博客网 时间:2024/05/26 05:53
<?php $servername = "localhost"$username = "username"$password = "password"$dbname = "myDB"// 创建连接 $conn = newmysqli($servername$username$password$dbname)// 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error)} else {$sql = "INSERT INTO MyGuests(firstname, lastname, email) VALUES(?, ?, ?)"// 为 mysqli_stmt_prepare() 初始化 statement 对象 $stmt =mysqli_stmt_init($conn)//预处理语句 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)} } ?>
0 0
原创粉丝点击