Current Location: Home> Latest Articles> Is it possible to call attr_get on a closed statement handle?

Is it possible to call attr_get on a closed statement handle?

M66 2025-05-29

When using PHP's mysqli extension for database operations, developers usually use prepared statements to improve security and performance. In some scenarios, we may try to call the mysqli_stmt::attr_get() method after the statement has been executed or even after the statement handle has been explicitly or implicitly closed. What happens in this case? Is it still work normally? This article will analyze this in detail.

What is mysqli_stmt::attr_get() ?

mysqli_stmt::attr_get() is a method in PHP that is used to obtain attributes related to a statement handle. The syntax is as follows:

 int mysqli_stmt::attr_get( int $attribute )

It is usually used to get attribute values, such as settings of buffered result sets ( MYSQLI_STMT_ATTR_CURSOR_TYPE ), preprocessing buffer size, etc.

Call behavior on closed statement handle

When the statement handle is closed (automatically cleaned after execution by mysqli_stmt::close() or script execution), its resources have actually been released. Calling any method on it at this time, including attr_get() , may lead to the following situations:

  1. Throw an error or warning <br> In PHP, calling a method on a freed object resource usually throws a warning or fatal error. For example:

     $stmt = $mysqli->prepare("SELECT * FROM users");
    $stmt->close();
    $cursorType = $stmt->attr_get(MYSQLI_STMT_ATTR_CURSOR_TYPE); // Warning or error
    

    The output may be similar to:

     Warning: mysqli_stmt::attr_get(): Couldn't fetch mysqli_stmt in /var/www/html/index.php on line 4
    

    This means that the handle has expired and no property acquisition can be performed.

  2. Return null or false
    Some PHP versions and configurations may not throw an error immediately, but return false or null . Although this will not interrupt script execution, it still indicates that the method cannot be executed successfully and the result is invalid.

Can it be used normally?

The conclusion is: No. Once the statement handle is closed, its internal state and resources have been destroyed and can no longer be used for any operations, including calling the attr_get() method. Trying to do this will result in script errors or logical exceptions.

Recommended practices

  1. Get the required properties before closing the handle <br> If you really need to get a certain property, make sure to do it before the close() call. For example:

     $stmt = $mysqli->prepare("SELECT * FROM users");
    $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_READ_ONLY);
    $cursorType = $stmt->attr_get(MYSQLI_STMT_ATTR_CURSOR_TYPE);
    $stmt->close();
    
  2. Avoid operation on failed objects <br> Always check the validity of the handle before the operation. You can encapsulate judgment logic to ensure the robustness of the code.

  3. Utilize exception handling mechanism <br> Turn on mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) to quickly catch and locate problems when errors occur.

Example: Use attr_get to get attributes

 <?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "user", "pass", "dbname");

$stmt = $mysqli->prepare("SELECT * FROM articles WHERE status = ?");
$stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_READ_ONLY);

$cursorType = $stmt->attr_get(MYSQLI_STMT_ATTR_CURSOR_TYPE);
echo "Cursor Type: " . $cursorType;

$stmt->bind_param("s", $status);
$status = 'published';
$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    echo $row['title'] . "<br>";
}

$stmt->close();
$mysqli->close();
?>

Note: If you plan to interact with the backend via URL, for example through AJAX request results, make sure the domain name is replaced with your own domain name such as https://m66.net/api/fetch_status.php to avoid cross-site problems.

Summarize

  • mysqli_stmt::attr_get() cannot be used on closed statement handles.

  • Calling this method should ensure that the handle is still active.

  • Improper use may lead to warnings or logic errors, affecting program stability.

  • Correctly managing resource lifecycles is part of good PHP programming practices.

By understanding these behaviors and following best practices, unnecessary errors can be effectively avoided and code robustness and maintainability can be improved.