
说明
1、绑定变量的SQL,使用问号标记可以接收参数的位置,当真正需要执行具体查询的时候,则使用具体值代替这些问号。
2、创建绑定变量SQL时,客户端向服务器发送SQL语言的原型。服务器方面收到这个SQL句子的框架后,分析并保存这个SQL句子的一部分执行计划,返回给客户SQL句子处理句柄。
绑定变量的SQL语句:
1 | INSERT INTO tbl(col1, col2, col3) VALUES (?, ?, ?);
|
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | $mysqli = new mysqli( "localhost" , "my_user" , "my_password" , "world" );
/* check connection */
if (mysqli_connect_errno()) {
printf( "Connect failed: %s\n" , mysqli_connect_error());
exit();
}
$city = "Amersfoort" ;
/* create a prepared statement */
if ($stmt = $mysqli-> prepare ( "SELECT District FROM City WHERE Name=?" )) {
/* bind parameters for markers */
$stmt->bind_param( "s" , $city);
/* execute query */
$stmt-> execute ();
//下面的变量为查询表中的字段命名的变量
$stmt->bind_result($district);
/* fetch value */
$stmt-> fetch ();
printf( "%s is in district %s\n" , $city, $district);
/* close statement */
$stmt-> close ();
}
/* close connection */
$mysqli-> close ();
?>
|
以上就是mysql绑定变量的介绍,希望对大家有所帮助。更多mysql学习指路:MySQL