(PHP 5, PHP 7)
mysqli::prepare -- mysqli_prepare — 准锟斤拷执锟斤拷一锟斤拷 SQL 锟斤拷锟�
锟斤拷锟斤拷锟斤拷锟斤拷锟�
锟斤拷锟教伙拷锟斤拷锟�
锟斤拷锟斤拷执锟斤拷 SQL 锟斤拷锟斤拷准锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷远锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷泻锟斤拷锟斤拷牟锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟街э拷值锟揭伙拷锟� SQL 锟斤拷洌拷锟街э拷侄锟� SQL 锟斤拷洹�
锟斤拷执锟斤拷锟斤拷锟街帮拷锟斤拷锟揭癸拷锟� mysqli_stmt_bind_param() 锟斤拷锟斤拷 锟斤拷占位锟斤拷锟斤拷锟斤拷锟斤拷锟叫绑定★拷 同锟斤拷锟斤拷锟节伙拷取锟斤拷锟街帮拷锟斤拷锟斤拷锟绞癸拷锟� mysqli_stmt_bind_result() 锟斤拷锟斤拷锟皆凤拷锟截碉拷锟斤拷值锟斤拷锟叫绑定★拷
link
锟斤拷锟皆癸拷锟教伙拷锟斤拷式锟斤拷锟斤拷mysqli_connect() 锟斤拷 mysqli_init() 锟斤拷锟截碉拷锟斤拷锟接憋拷识锟斤拷
query
SQL 锟斤拷洹�
Note:
锟斤拷锟斤拷要锟斤拷锟斤拷锟侥┪诧拷锟斤拷臃趾牛锟�;锟斤拷 锟斤拷锟斤拷 \g 锟斤拷锟斤拷锟斤拷锟斤拷
SQL 锟斤拷锟斤拷锌锟斤拷园锟斤拷锟揭伙拷锟斤拷锟斤拷叨锟斤拷锟绞号o拷?锟斤拷 锟斤拷示锟斤拷锟侥诧拷锟斤拷锟斤拷
Note:
SQL 锟斤拷锟斤拷校锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷囟锟斤拷锟轿伙拷贸锟斤拷锟斤拷屎挪锟斤拷锟秸嘉伙拷锟斤拷锟� 锟斤拷锟界,锟斤拷 INSERT 锟斤拷锟斤拷械锟� VALUES() 锟接撅拷锟叫匡拷锟斤拷使锟矫诧拷锟斤拷占位锟斤拷锟斤拷锟斤拷锟斤拷示锟斤拷应锟叫碉拷值锟斤拷 也锟斤拷锟斤拷锟斤拷 WHERE 锟街撅拷锟斤拷使锟矫诧拷锟斤拷锟斤拷锟斤拷示 要锟斤拷锟叫比较碉拷值锟斤拷
锟斤拷锟角o拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫的地凤拷锟斤拷锟斤拷锟斤拷使锟矫诧拷锟斤拷占位锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷诒锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� SQL 锟斤拷锟斤拷械谋锟绞段伙拷茫锟� 锟酵诧拷锟斤拷锟斤拷使锟矫诧拷锟斤拷占位锟斤拷 SELECT 锟斤拷锟斤拷械锟斤拷锟斤拷锟斤拷筒锟斤拷锟斤拷锟绞癸拷貌锟斤拷锟斤拷锟� 锟斤拷锟解,锟斤拷锟斤拷 = 锟斤拷锟斤拷锟斤拷锟竭硷拷锟饺较诧拷锟斤拷也锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟洁都使锟矫诧拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷诮锟斤拷锟� SQL 锟斤拷时锟斤拷筒锟街拷锟斤拷锟斤拷锟轿硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿★拷 也锟斤拷锟斤拷锟斤拷锟斤拷 NULL 锟斤拷锟斤拷锟斤拷使锟斤拷锟绞号o拷 也锟斤拷锟斤拷说锟斤拷锟斤拷锟斤拷写锟缴o拷? IS NULL锟斤拷 一锟斤拷锟斤拷裕锟斤拷锟斤拷锟揭仓伙拷锟斤拷锟斤拷锟斤拷锟斤拷莶锟斤拷锟斤拷锟紻ML锟斤拷锟斤拷锟斤拷校锟� 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟捷讹拷锟藉(DDL锟斤拷锟斤拷锟斤拷小锟�
mysqli_prepare() 锟斤拷锟斤拷一锟斤拷 statement 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷蚍祷锟� FALSE
锟斤拷
Example #1 mysqli::prepare() 锟斤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷锟�
<?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";
/* 锟斤拷锟斤拷一锟斤拷预锟斤拷锟斤拷 SQL 锟斤拷锟� */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
/* 锟斤拷锟节诧拷锟斤拷占位锟斤拷锟斤拷锟叫诧拷锟斤拷值锟斤拷 */
$stmt->bind_param("s", $city);
/* 执锟叫诧拷询 */
$stmt->execute();
/* 锟斤拷锟斤拷询锟斤拷锟斤拷蠖ǖ锟斤拷锟斤拷锟� */
$stmt->bind_result($district);
/* 锟斤拷取锟斤拷询锟斤拷锟街� */
$stmt->fetch();
printf("%s is in district %s\n", $city, $district);
/* 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 */
$stmt->close();
}
/* 锟截憋拷锟斤拷锟斤拷 */
$mysqli->close();
?>
锟斤拷锟教伙拷锟斤拷锟�
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 锟斤拷锟斤拷锟斤拷锟� */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$city = "Amersfoort";
/* 锟斤拷锟斤拷一锟斤拷预锟斤拷锟斤拷 SQL 锟斤拷锟� */
if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name=?")) {
/* 锟斤拷锟节诧拷锟斤拷占位锟斤拷锟斤拷锟叫诧拷锟斤拷值锟斤拷 */
mysqli_stmt_bind_param($stmt, "s", $city);
/* 执锟叫诧拷询 */
mysqli_stmt_execute($stmt);
/* 锟斤拷锟斤拷询锟斤拷锟斤拷蠖ǖ锟斤拷锟斤拷锟� */
mysqli_stmt_bind_result($stmt, $district);
/* 锟斤拷取锟斤拷询锟斤拷锟街� */
mysqli_stmt_fetch($stmt);
printf("%s is in district %s\n", $city, $district);
/* 锟截憋拷锟斤拷锟斤拷锟� */
mysqli_stmt_close($stmt);
}
/* 锟截憋拷锟斤拷锟斤拷 */
mysqli_close($link);
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Amersfoort is in district Utrecht