Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_stmt_attr_set() Function.
The built-in PHP mysqli_stmt_attr_set() function is used to modify the behavior of a prepared statement. This built-in function may be called multiple times to set several attributes.
The built-in PHP mysqli_stmt_attr_set() function is used to modify the behavior of a prepared statement. This built-in function may be called multiple times to set several attributes.
Syntax
Following below is the syntax to use this function -
mysqli_stmt_attr_set($stmt, $attr, $mode);
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | stmt(Mandatory) This is an object representing a prepared statement. |
2 | attr(Mandatory) This is an integer value representing the attribute you want to set to the given statement which can be one of the following −
|
3 | mode(Mandatory) This is an integer value which you want to assign to the attribute. |
Return Value
This built-in PHP function returns TRUE on success and FALSE on failure.
PHP Version
This PHP function was first introduced in PHP version 5 and works in all the later versions.
Example1
The following below is an example which demonstrates the usage of the built-in PHP mysqli_stmt_attr_set() function (in a procedural style) -
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; mysqli_query($con, $query); print("Table Created.....\n"); //insert into Test values('Kennedy', 27); $stmt = mysqli_prepare($con, "INSERT INTO Test values(?, ?)"); mysqli_stmt_bind_param($stmt, "si", $Name, $Age); $Name = 'Kennedy'; $Age = 27; print("Record Inserted.....\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); //Executing the statement mysqli_stmt_execute($stmt); //Closing the statement mysqli_stmt_close($stmt); //Closing the connection mysqli_close($con); ?>
Output
When the above code is executed, it will produce the following result -
Table Created..... Record Inserted..... Successful..... Value: 1
Example2
In an object oriented style the syntax of this function is $stmt->close(); Following is the example of this function in an object oriented style -
<?php //Creating a connection $con = new mysqli("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; $con -> query($query); print("Table Created.....\n"); //insert into Test values('Kennedy', 27);//,('Paul', 30),('Justice', 28)"; $stmt = $con -> prepare( "INSERT INTO Test values(?, ?)"); $stmt -> bind_param("si", $Name, $Age); $Name = 'Kennedy'; $Age = 27; print("Record Inserted.....\n"); //Setting the 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); //Executing the statement $stmt->execute(); //Closing the statement $stmt->close(); //Closing the connection $con->close(); ?>
Output
When the above code is executed, it will produce the following result -
Table Created..... Record Inserted..... Successful..... Value: 1
Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP mysqli_stmt_bind_param() Function.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.