• PHP Video Tutorials

PHP mysqli_stmt_attr_get() Function



Definition and Usage

You can create a prepared statement, which has parameter markers ("?") in case of values using the mysqli_prepare() function. Once you prepare a statement, you need to bind values to the parameters of the created statement using the mysqli_stmt_bind_param() function.

You can set various attributes to the statement which changes its behaviour using the mysqli_stmt_attr_set() function.

The mysqli_stmt_attr_get() function accepts a statement object and an attribute and returns the current value of the given attribute.

Syntax

mysqli_stmt_attr_get($stmt, $attr);

Parameters

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 −

  • MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH

  • MYSQLI_STMT_ATTR_CURSOR_TYPE

  • MYSQLI_STMT_ATTR_PREFETCH_ROWS

Return Values

The PHP mysqli_stmt_attr_get() function returns the value of the specified attribute on success and false if the given the attribute is not found.

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_stmt_attr_set() function (in 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('Raju', 25);
   $stmt = mysqli_prepare($con, "INSERT INTO Test values(?, ?)");
   mysqli_stmt_bind_param($stmt, "si", $Name, $Age);
   $Name = 'Raju';
   $Age = 25;
   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);
?>

This will produce following result −

Table Created.....
Record Inserted.....
Successful.....
Value: 1

Example

In object oriented style the syntax of this function is $stmt->close(); Following is the example of this function in object oriented style $minus;

<?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('Raju', 25);//,('Rahman', 30),('Sarmista', 27)";
   $stmt = $con -> prepare( "INSERT INTO Test values(?, ?)");
   $stmt -> bind_param("si", $Name, $Age);
   $Name = 'Raju';
   $Age = 25;
   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();
?>

This will produce following result −

Table Created.....
Record Inserted.....
Successful.....
Value: 1
php_function_reference.htm
Advertisements