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:
Return value:
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.