In Web development, the storage and processing of data is usually a core task, especially in database operations and data format processing. PHP's mysqli extension provides powerful database interaction functions, and the JSON format, as a lightweight data exchange format, has been widely used in different Web development scenarios.
In this article, we will show how to use the mysqli::stmt_init function in conjunction with JSON data storage to achieve efficient data processing. stmt_init is a method in the mysqli class to initialize a preparation statement object, while the JSON data format is suitable for storing and processing structured data. Through this combination, the efficiency of database operations can be improved and data access and processing can be simplified.
mysqli::stmt_init is a method in the mysqli class that is used to initialize a new preparation statement. Prepared Statements allow you to precompile SQL query statements, thereby preventing SQL injection attacks and improving query performance.
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Initialize the preparation statement object
$stmt = $mysqli->stmt_init();
?>
In this example, we create a new instance of mysqli and connect to the database. Next, we call stmt_init to initialize a preparation statement object.
Data stored in JSON format is very convenient in databases, especially suitable for scenarios where structured or nested data is required. Let's assume that there is a user table with a preferences field used to store user settings data in JSON format.
First, we create a SQL query containing JSON data and insert the data using a preparation statement.
<?php
// Suppose we have the following JSON data
$user_data = array(
"theme" => "dark",
"notifications" => true,
"language" => "en"
);
// Will PHP Convert array to JSON Format
$json_data = json_encode($user_data);
// SQL Query statement
$query = "INSERT INTO users (username, preferences) VALUES (?, ?)";
// Initialization preparation statement
if ($stmt->prepare($query)) {
// Bind parameters
$stmt->bind_param("ss", $username, $json_data);
// Set username and JSON data
$username = "john_doe";
// Execute a query
$stmt->execute();
echo "New record created successfully!";
} else {
echo "Error preparing statement: " . $stmt->error;
}
?>
In this example, we first convert a PHP array (including user settings) to a JSON string and bind the parameters using the bind_param method. The data is then inserted into the database by executing the preparation statement.
In addition to inserting data, we can also read and process JSON data from the database. Suppose we want to get the user's settings from the users table and parse the JSON data for further operation.
<?php
// 查询data库Get用户data
$query = "SELECT username, preferences FROM users WHERE username = ?";
if ($stmt->prepare($query)) {
// Bind parameters
$stmt->bind_param("s", $username);
// Set username
$username = "john_doe";
// Execute a query
$stmt->execute();
// Get results
$stmt->bind_result($username, $json_data);
while ($stmt->fetch()) {
// Analysis JSON data
$preferences = json_decode($json_data, true);
// Output user settings
echo "Theme: " . $preferences['theme'] . "<br>";
echo "Notifications: " . ($preferences['notifications'] ? 'Enabled' : 'Disabled') . "<br>";
echo "Language: " . $preferences['language'] . "<br>";
}
} else {
echo "Error preparing statement: " . $stmt->error;
}
?>
In this query example, we read the JSON data stored in the preferences field from the database and convert it into a PHP array using the json_decode function. We can then easily access the data in it and display it on the page.
In some application scenarios, it may be necessary to interact with external systems through URLs. Suppose we need to get JSON data from an external API and store it in the database. We can use file_get_contents or cURL to get data from the API and store it in JSON format.
<?php
// Assume we API Get JSON data
$api_url = "https://m66.net/api/user/preferences"; // Replace with actual API URL
// Get API response
$response = file_get_contents($api_url);
// WillresponsedataAnalysis为 PHP Array
$preferences_data = json_decode($response, true);
// WillGet到的 JSON data插入data库
$query = "INSERT INTO users (username, preferences) VALUES (?, ?)";
if ($stmt->prepare($query)) {
// Bind parameters
$stmt->bind_param("ss", $username, $json_data);
// Set username and JSON data
$username = "john_doe";
$json_data = json_encode($preferences_data);
// Execute a query
$stmt->execute();
echo "User preferences stored successfully!";
} else {
echo "Error preparing statement: " . $stmt->error;
}
?>
In this example, we get user preference data from m66.net 's API and store it in the database in JSON format.
By combining mysqli::stmt_init and JSON data storage, we can process and store structured data efficiently. Using preparation statements can not only improve the efficiency of database operations, but also effectively prevent SQL injection attacks. The JSON format provides us with a flexible and easy-to-operate data storage method, which is ideal for storing information such as configuration or user preferences.
In practical applications, query statements and data storage methods can be adjusted according to different needs, making the entire data processing process more efficient and secure.