Php/func mysqli prepare

来自菜鸟教程
跳转至:导航、​搜索

PHP mysqli prepare()函数

MySQL PHP MySQLi参考

示例-面向对象的样式

准备要执行的SQL语句:

<?php

$mysqli = new mysqli("localhost","my_user","my_password","my_db");


// Check connection
if ($mysqli -> connect_errno) {

  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  
  exit();

  }

// prepare and bind
$stmt = $mysqli -> prepare("INSERT INTO 
  MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt -> 
  bind_param("sss", $firstname, $lastname, $email);

// set parameters and 
  execute
$firstname 
  = "John";
$lastname = "Doe";
$email = "john@example.com";
$stmt -> execute();


$firstname = "Mary";
$lastname = "Moe";
$email = "mary@example.com";

  $stmt -> execute();

echo "New records created successfully";

$stmt -> close();
$mysqli -> close();

?>



定义和用法

prepare()/ mysqli_prepare()函数用于准备要执行的SQL语句。

句法

面向对象的样式:

$mysqli -> prepare(query)

程序风格:

mysqli_prepare(connection, query)

参数值

参数 描述
connection 需要。指定要使用的MySQL连接
query 需要。指定一个SQL查询。

注意: 不要在查询末尾添加分号!

技术细节

返回值: 成功的声明对象。失败时为假
PHP版本: 5+

MySQL PHP MySQLi参考