English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_stmt_attr_set() function is used to modify the behavior of prepared statements.
You can use the mysqli_prepare() function to create a prepared statement with parameter markers ("?") (if any). After preparing the statement, you need to usemysqli_stmt_bind_param()Function binds values to the parameters of the created statement.
You can use the mysqli_stmt_attr_set() function to set various attributes for a statement to change its behavior.
mysqli_stmt_attr_set($stmt, $attr, $mode);
Serial Number | Parameters and Description |
---|---|
1 | stmt (required) This is the object representing the prepared statement. |
2 | attr (required) This is an integer value representing the attribute you want to set for the given statement. The attribute can be one of the following values:
|
3 | mode (required) This is the integer value to be assigned to the attribute. |
The PHP mysqli_stmt_attr_set() function returns a boolean value, which istrue, failing whenfalse.
This function was initially introduced in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_stmt_attr_set()Function Usage (Procedural Style)-
<?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();.The following 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