English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
mysqli_stmt_attr_set()函数用于修改预处理语句的行为。
您可以使用mysqli_prepare()函数创建一个预处理好的语句,该语句具有参数标记(“?”)(如果有值)。准备语句后,需要使用mysqli_stmt_bind_param()函数将值绑定到所创建语句的参数。
您可以使用mysqli_stmt_attr_set()函数为语句设置各种属性,以更改其行为。
mysqli_stmt_attr_set($stmt, $attr, $mode);
序号 | 参数及说明 |
---|---|
1 | stmt(必需) 这是表示准备好的语句的对象。 |
2 | attr(必需) 这是一个整数值,表示您要设置给定语句的属性,该属性可以是下列值之一:
|
3 | mode(必需) 这是要分配给属性的整数值。 |
PHP mysqli_stmt_attr_set()函数返回一个布尔值,成功时为true,失败时为false。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_attr_set()函数的用法(面向过程风格)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT); mysqli_query($con, $query); print("Create table.....\n"); //insert into Test values('Raju', 25); $stmt = mysqli_prepare($con, "INSERT INTO Test values(?, ?)"); mysqli_stmt_bind_param($stmt, "si", $Name, $Age); $Name = 'Raju'; $Age = 25; print("Insert record.....\n"); $res = mysqli_stmt_attr_set($stmt, MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, TRUE); if($res){ print("Successful.....\n"); }else{ print("Failed.....\n"); } $val = mysqli_stmt_attr_get($stmt, MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH); print("Value: ").$val; //Execute statement mysqli_stmt_execute($stmt); //End statement mysqli_stmt_close($stmt); //Close connection mysqli_close($con); ?>
Output result
Create table..... Insert record..... Successful..... Value: 1
In the object-oriented style, the syntax of this function is$stmt->close();.Here is an example of setting this function's attribute in an object-oriented style;
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT); $con ->query($query); print("Create table.....\n"); //insert into Test values('Raju', 25);//,('Rahman', 30),('Sarmista', 27); $stmt = $con ->prepare("INSERT INTO Test values(?, ?"); $stmt ->bind_param("si", $Name, $Age); $Name = 'Raju'; $Age = 25; print("Insert record.....\n"); //Set attribute $res = $stmt;->attr_set(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, TRUE); if($res){ print("Successful.....\n"); }else{ print("Failed.....\n"); } $val = $stmt;->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH); print("Value: ").$val; //Execute statement $stmt->execute(); //End statement $stmt->close(); //Close connection $con->close(); ?>
Output result
Create table..... Insert record..... Successful..... Value: 1