Current Location: Home> Latest Articles> How to use attr_get to cooperate with log debugging preprocessing statements

How to use attr_get to cooperate with log debugging preprocessing statements

M66 2025-05-28

Prepared statements are a powerful tool when using PHP's mysqli extension for database operations. It not only effectively prevents SQL injection, but also improves the maintainability and performance of the code. When debugging and optimizing these preprocessing statements, the mysqli_stmt::attr_get function provides an in-depth way to understand the internal state of the statement. This article will explain its usage and practical applications in detail.

1. What is mysqli_stmt::attr_get ?

mysqli_stmt::attr_get is a method of the mysqli_stmt class that is used to obtain attribute values ​​related to a statement handle. These attribute values ​​are usually used to adjust or view some underlying behaviors performed by a statement, such as buffering or update counts. This method is especially useful when debugging complex queries or optimizing performance bottlenecks.

 int mysqli_stmt::attr_get ( int $attr )
  • $attr : attribute constants that need to be queried, such as MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH , etc.

  • Return value: Returns the value of the attribute when successful, and returns false when failure.

2. Common uses examples

1. Get the update maximum length ( MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH )

This property is used to control whether store_result() updates the maximum length of the field. Enabling this property allows you to more accurately know the maximum data length for each column, which is especially useful for formatting output.

 $mysqli = new mysqli("localhost", "user", "password", "database");

$stmt = $mysqli->prepare("SELECT name FROM users");
$stmt->execute();
$stmt->store_result();

$maxLength = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
if ($maxLength) {
    echo "Properties enabled:Maximum column length updated\n";
} else {
    echo "Properties not enabled:Maximum column length not updated\n";
}

In this way, it can be judged whether the column length information returned by mysqli_stmt ::result_metadata() can be relied on.

2. Debug fetch behavior and performance bottlenecks

When you find poor performance using bind_result() and fetch() in a loop, it may be related to buffering mode. Although attr_get cannot directly obtain whether buffering is enabled, you can use store_result() and the return value of this property to determine whether the buffering state is properly managed, thereby guiding whether use_result() is enabled to save memory.

 $stmt = $mysqli->prepare("SELECT large_column FROM big_table");
$stmt->execute();
$stmt->store_result();

if ($stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH)) {
    echo "Maximum length of update column is enabled,May increase memory usage。\n";
} else {
    echo "Update column length is not enabled,fetch Probably faster but inaccurate length。\n";
}

3. Performance optimization suggestions

  1. Enable UPDATE_MAX_LENGTH only if necessary : ​​If you just traverse the data instead of relying on the maximum length information for each column, you don't need to enable this property to reduce unnecessary performance overhead.

  2. Dynamically adjust attributes with attr_set : Use attr_set to set the attribute value before execution, and verify whether the configuration takes effect through attr_get after execution.

 $stmt->attr_set(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, true);
...
$val = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
  1. Select the buffering mode based on actual business scenarios : For scenarios with large data volume and tight memory, it is recommended to use non-buffering mode ( use_result() ). At this time, the maximum column length returned by attr_get will be inaccurate, but the performance will be higher.

4. Debugging with log output

In the development environment, you can write the results of attr_get to the log file for easier subsequent analysis.

 file_put_contents('/var/log/mysqli_debug.log', "UPDATE_MAX_LENGTH: " . $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH) . "\n", FILE_APPEND);

This method is particularly suitable for slow query debugging and behavioral analysis in complex systems, helping developers quickly locate the source of problems.

5. Real application case: judging field length in the pagination system

Suppose you develop a user list paging system on m66.net and need to align the field column widths in the table. At this time, you can use attr_get and store_result to get the maximum length of each column:

 $stmt = $mysqli->prepare("SELECT username, email FROM users LIMIT ?, ?");
$stmt->bind_param("ii", $offset, $limit);
$stmt->execute();
$stmt->store_result();

if ($stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH)) {
    $meta = $stmt->result_metadata();
    while ($field = $meta->fetch_field()) {
        echo "Fields {$field->name} The maximum length is:{$field->max_length}\n";
    }
}

In this way, the column width of HTML tables can be dynamically controlled to improve the consistency of the user interface.

Summarize