Current Location: Home> Function Categories> mysqli_stmt::attr_get

mysqli_stmt::attr_get

(mysqli_stmt_attr_get) is used to obtain the current value of the statement attribute
Name:mysqli_stmt::attr_get
Category:MySQLi
Programming Language:php
One-line Description:Get the value of the specified preprocessing statement attribute

Function name: mysqli_stmt::attr_get()

Applicable version: PHP 5 >= 5.3.0, PHP 7, PHP 8

Usage: The mysqli_stmt::attr_get() function is used to get the value of the specified preprocessing statement attribute. This function needs to be called on the mysqli_stmt object.

Syntax: mixed mysqli_stmt::attr_get ( int $attr )

parameter:

  • attr: A constant of the preprocessing statement attribute, indicating the attribute to be obtained. Common properties include:
    • MYSQLI_STMT_ATTR_CURSOR_TYPE: The cursor type of the preprocessing statement.
    • MYSQLI_STMT_ATTR_PREFETCH_ROWS: The number of rows obtained from the server each time the preprocessing statement is obtained.
    • MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH: Specifies whether the maximum length of the relevant binding in the preprocessing statement is automatically updated when the length of the bound variable changes.

Return value:

  • Returns the current value of the attribute when successful, and returns FALSE when failure.

Example:

prepare("SELECT name, age FROM users WHERE id = ?"); // Get the cursor type of the preprocessing statement $cursorType = $stmt->attr_get(MYSQLI_STMT_ATTR_CURSOR_TYPE); echo "cursor type: " . $cursorType . "\n"; // Get the number of rows obtained by preprocessing statement $prefetchRows = $stmt->attr_get(MYSQLI_STMT_ATTR_PREFETCH_ROWS); echo "get the number of rows each time: " . $prefetchRows . "\n"; // Get the number of rows obtained by preprocessing statement $prefetchRows = $stmt->attr_get(MYSQLI_STMT_ATTR_PREFETCH_ROWS); echo "get the number of rows each time: " . $prefetchRows . "\n"; // Get whether the preprocessing statement automatically updates the maximum length of the bound variable $updateMaxLength = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH); echo "Is the maximum length automatically updated: " . ($updateMaxLength ? "Yes" : "No") . "\n"; // Close the preprocessing statement and database connection $stmt->close(); $mysqli->close(); ?>

In the above example, we first create a database connection and then prepare a preprocessing statement. Next, we use the mysqli_stmt::attr_get() function to obtain the cursor type of the preprocessing statement, the number of rows each time, and whether to automatically update the maximum length of the bound variable. Finally, the preprocessing statement and database connection are closed.

Note that the parameters MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_STMT_ATTR_PREFETCH_ROWS and MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH in the example are predefined constants indicating corresponding attributes. In actual use, you can select the appropriate attributes to obtain them according to your needs.