English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_stmt_attr_get() function is used to get the current value of the statement attribute
You can use the mysqli_prepare() function to create a prepared statement with parameter markers ("?") (if any). After preparing the statement, you need to use the mysqli_stmt_bind_param() function to bind 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.
The mysqli_stmt_attr_get() function accepts a statement object and an attribute and returns the current value of the given attribute.
mysqli_stmt_attr_get($stmt, $attr);
Serial number | Parameters and descriptions |
---|---|
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, which can be one of the following values:
|
PHP mysqli_stmt_attr_get() function successfully returns the value of the specified attribute, if the given attribute is not found, it returnsfalse.
This function was initially introduced in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_stmt_attr_set()Usage of the function (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 this function 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