Current Location: Home> Latest Articles> The complete process of setting and obtaining attribute values ​​in combination with mysqli_stmt::attr_set

The complete process of setting and obtaining attribute values ​​in combination with mysqli_stmt::attr_set

M66 2025-05-22

In PHP, mysqli_stmt::attr_set and mysqli_stmt::attr_get are methods used to set and obtain the properties of mysqli_stmt (preprocessing statements). Although these two methods are not often used frequently in actual development, they show strong flexibility in adjusting the behavior of statements in specific scenarios. This article will demonstrate how to set and get property values ​​using these two functions through a complete process.

1. Prerequisites

Before you begin, make sure you already have the following environment:

  • Install and enable the PHP running environment with mysqli extension;

  • A running MySQL database;

  • Accessible database username, password and database name;

  • A test data sheet is used to demonstrate preprocessing statements.

2. Preparation

Let's assume you already have the following database connection configuration:

 $mysqli = new mysqli("localhost", "username", "password", "test_db");
if ($mysqli->connect_errno) {
    die("Connection failed: " . $mysqli->connect_error);
}

Then, prepare a simple test table:

 CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

3. Set attribute value: mysqli_stmt::attr_set

Property settings are generally used to control the execution behavior of preprocessing statements. For example, using the STMT_ATTR_UPDATE_MAX_LENGTH property can automatically update the maximum length of the resulting field when a SELECT query is executed.

 $stmt = $mysqli->prepare("SELECT name FROM users");
if (!$stmt) {
    die("Preprocessing failed: " . $mysqli->error);
}

// Set properties:Let the maximum field length be updated after the query is executed
$attr = mysqli_stmt::ATTR_UPDATE_MAX_LENGTH;
$success = $stmt->attr_set($attr, true);

if (!$success) {
    echo "Set properties失败\n";
}

4. Execute the statement and get the attribute value: mysqli_stmt::attr_get

After setting the attribute, we can execute the statement, and then obtain the current value of the attribute through attr_get to verify whether the setting is successful.

 $stmt->execute();
$result = $stmt->get_result();

$length_updated = $stmt->attr_get($attr);
echo "Is the maximum field length updated?: " . ($length_updated ? "yes" : "no") . "\n";

5. Complete code example

 $mysqli = new mysqli("localhost", "username", "password", "test_db");
if ($mysqli->connect_errno) {
    die("Connection failed: " . $mysqli->connect_error);
}

$stmt = $mysqli->prepare("SELECT name FROM users");
if (!$stmt) {
    die("Preprocessing failed: " . $mysqli->error);
}

// Set properties:Update maximum length
$attr = mysqli_stmt::ATTR_UPDATE_MAX_LENGTH;
if (!$stmt->attr_set($attr, true)) {
    die("Set properties失败");
}

$stmt->execute();
$result = $stmt->get_result();

if ($stmt->attr_get($attr)) {
    echo "The maximum field length has been updated。\n";
} else {
    echo "The maximum field length is not updated。\n";
}

while ($row = $result->fetch_assoc()) {
    echo "username: " . $row['name'] . "\n";
}

6. Things to note

  1. Limited attribute support : As of the current version, mysqli_stmt only supports a very small number of attributes, such as ATTR_UPDATE_MAX_LENGTH . Using unsupported properties will not take effect.

  2. Database Driver Difference : The behavior of certain properties may depend on the underlying MySQL client library version.

  3. Compatibility issues : The attr_set and attr_get methods have been officially introduced since PHP 8.1.0. Old versions of PHP are not supported, so please pay attention to version compatibility.

7. Summary

Through the process of this article, we learned how to set properties using the mysqli_stmt::attr_set method and get their status using mysqli_stmt::attr_get . This method is especially useful when optimizing query or debugging behavior. Although there are limited use scenarios, mastering them will help us to have a deeper understanding of PHP's database interface mechanism.

In project development, if you need to interact efficiently with the database through PHP, you might as well explore and master these low-frequency but practical APIs, which will help build more stable and flexible back-end logic.