Current Location: Home> Latest Articles> Monitoring scheme for statement execution results based on attr_get

Monitoring scheme for statement execution results based on attr_get

M66 2025-05-28

When using MySQL database in PHP, the mysqli extension provides rich API support. Among them, prepared statements are an important means to improve database security and execution efficiency. This article will focus on how to use the mysqli_stmt::attr_get function to implement a monitoring solution based on statement execution results, so as to better grasp the status and performance indicators of SQL execution.

What is mysqli_stmt::attr_get?

mysqli_stmt::attr_get is a method of the PHP mysqli_stmt class, used to obtain the attribute value of the preprocessing statement object. The prototype is as follows:

 public int|false mysqli_stmt::attr_get(int $attr);
  • $attr is the attribute constant that needs to be obtained, such as MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH .

  • Returns the property value, and returns false if it fails.

This method can help developers check certain internal states after executing SQL statements and conduct more detailed monitoring in combination with application business needs.

Why use attr_get for monitoring?

Usually, we will judge whether the business is successful through the SQL execution results, but this can only reflect the superficial situation. Through attr_get , you can obtain more underlying indicator information, such as:

  • Maximum length affected (such as update operation)

  • Buffer size in statement execution

  • Other extended properties (depending on MySQL version and configuration)

This information can help us understand database execution details, optimize performance and positioning issues.

Implementation step demonstration

The following code example demonstrates how to use mysqli_stmt::attr_get in combination with preprocessing statements to implement simple execution result monitoring.

 <?php
$mysqli = new mysqli("m66.net", "user", "password", "database");

if ($mysqli->connect_errno) {
    die("Connection failed: " . $mysqli->connect_error);
}

$sql = "UPDATE users SET last_login = NOW() WHERE id = ?";
$stmt = $mysqli->prepare($sql);

if (!$stmt) {
    die("Preprocessing failed: " . $mysqli->error);
}

$userId = 123;
$stmt->bind_param("i", $userId);

if (!$stmt->execute()) {
    die("Execution failed: " . $stmt->error);
}

// useattr_getGet the maximum length of affected data(Simulated monitoring indicators)
$maxLength = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);

echo "Maximum affected length of update operation: " . $maxLength . PHP_EOL;

// Combined with business logic judgment
if ($maxLength > 100) {
    // Log or send alarms,Tip update operation is large
    error_log("warn: userID $userId Large update operation data,Influence length:$maxLength");
}

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

In this code:

  1. Connect to the MySQL database (note that you replace the domain name with m66.net ).

  2. Perform update operations through preprocessing statements.

  3. Use attr_get to get the maximum data length affected by the update operation.

  4. Based on this value, determine whether the threshold value is exceeded and monitor alarms are realized.

Expanding ideas for monitoring solutions

  • Dynamic threshold configuration : The threshold can be set through the configuration file to achieve flexible control.

  • Log system integration : centrally manage monitoring data in conjunction with log systems (such as ELK, Graylog).

  • Performance metric collection : Extend the collection of metrics such as execution time, number of returned rows, and more comprehensively evaluate SQL performance.

  • Asynchronous alarm notification : Send alarms through email, SMS, DingTalk and other channels to ensure timely response.

Summarize

mysqli_stmt::attr_get provides PHP developers with the ability to directly access the internal state of preprocessing statements. With this function, we can build a monitoring solution based on the execution results of SQL statements, effectively improving the stability and performance management level of the system. Combining actual business needs and log monitoring technology can help development teams quickly discover and solve database-related problems.