当前位置:Gxlcms > 数据库问题 > MySQLi面向过程实践---预处理

MySQLi面向过程实践---预处理

时间:2021-07-01 10:21:17 帮助过:31人阅读

<?php 
	$conn=mysqli_connect("localhost","root","root","test");
	
	//设置字符集	
	mysqli_set_charset($conn,"utf8");

	//预处理,注意返回的类型是mysqli_stmt
	$stmt=mysqli_prepare($conn,"insert into aaa values (?,?)");

	// 绑定参数,第二个参数,请看下文注解
	// 注意,在$id,$name等变量的位置,不能出现常亮
	// mysqli_stmt_bind_param($stmt,"is",5,‘aaaaa‘);写法是错误的
	mysqli_stmt_bind_param($stmt,"is",$id,$name);

	// 参数赋值
	$id="99";$name="xxxxxx";

	//执行预处理
	mysqli_stmt_execute($stmt);

	//关闭预处理,
	//注意关闭的不是数据库连接,而是“预处理”
	mysqli_stmt_close($stmt);

	//关闭数据库连接
	mysqli_close($conn);
 ?>

这里注意绑定参数的时候,

bool mysqli_stmt_bind_param ( mysqli_stmt $stmt , string $types , mixed &$var1 [, mixed &$... ] )

第二个参数是类型(type),类型有4个类型,分别是:

  i(整型integer)、

  d(双精度浮点数double)、

  s(字符串string)、

  b(二进制大数据块blob)

每一个类型,即每个字母(i/d/s/b)对应一个参数,每个参数的位置所对应的类型要正确,顺序别颠倒

MySQLi面向过程实践---预处理

标签:错误   bin   大数   ini   ble   cal   bool   integer   prepare   

人气教程排行