mysqli_prepare() is used to prepare a SQL statement, which returns a mysqli_stmt statement object. For example:
$mysqli = new mysqli("m66.net", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$sql = "SELECT * FROM users WHERE email = ?";
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
die("Preprocessing failed: " . $mysqli->error);
}
Here, ? is the parameter placeholder, and the specific value will be bound through bind_param in the future.
The mysqli_stmt object supports the acquisition and setting of some properties, allowing control of the behavior of preprocessing statements. PHP provides two related methods:
attr_get($attr) : Gets the current value of the specified attribute.
attr_set($attr, $value) : Sets the value of the specified attribute.
For example, the attribute constants supported by MySQLi include:
MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH : Controls whether to update the maximum length.
MYSQLI_STMT_ATTR_CURSOR_TYPE : Set cursor type (such as non-cursor, read-only cursor, etc.).
MYSQLI_STMT_ATTR_PREFETCH_ROWS : The number of prefetched rows.
Suppose we want to use cursors to process large amounts of data and avoid loading all result sets at once, we can set cursor properties:
$sql = "SELECT * FROM large_table WHERE category = ?";
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
die("Preprocessing failed: " . $mysqli->error);
}
// Set cursor type to MYSQLI_CURSOR_TYPE_READ_ONLY
$stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_READ_ONLY);
// Bind parameters
$category = 'books';
$stmt->bind_param("s", $category);
$stmt->execute();
// Get the current cursor type
$currentCursorType = $stmt->attr_get(MYSQLI_STMT_ATTR_CURSOR_TYPE);
echo "The current cursor type is: " . $currentCursorType . PHP_EOL;
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
print_r($row);
}
$stmt->close();
$mysqli->close();
The above example demonstrates how to set the cursor type after preparing the statement and verify the current attribute with attr_get() .
Before calling execute() , set all required properties as much as possible, such as cursor type, prefetched row count, etc., to ensure that they can take effect during execution.
Both preprocessing and property settings may fail. Be sure to detect the return value and handle errors well to avoid running-time crashes.
Cursor types affect memory and performance, and especially when dealing with large result sets, using read-only cursors ( MYSQLI_CURSOR_TYPE_READ_ONLY ) is usually a better choice.
Some properties may not be supported on older versions of MySQL servers or PHP versions. You must confirm the support of the target environment during development.
Reasonably combining mysqli_prepare() , mysqli_stmt::attr_get and attr_set can make your database operations more flexible and efficient while maintaining the security of your code. By setting appropriate properties, especially cursors and prefetched row counts, performance and resource usage can be significantly optimized when dealing with large amounts of data.