When developing PHP website applications, we often need to dynamically display the data obtained from the database to the front end, and we hope that after the user refreshes or leaves the page, this data can be stored persistently to prevent loss. Combining the mysqli_result function with PHP's Session storage can achieve this goal very easily. This article will introduce in detail how to use these two for dynamic display and persistence of data.
mysqli_result is a class in PHP that handles MySQL query results. It is usually obtained through the mysqli_query or mysqli_store_result function, and provides a variety of ways to traverse the query results. Here is a simple example:
<?php
// Connect to MySQL database
$mysqli = new mysqli("localhost", "user", "password", "database");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Execute a query
$query = "SELECT id, name, email FROM users";
$result = $mysqli->query($query);
// use mysqli_result Classes to traverse query results
if ($result->num_rows > 0) {
// Output data for each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 result";
}
$mysqli->close();
?>
In the above code, the $result variable contains the results of the database query. We use the fetch_assoc method to extract the data row by row and display it.
Session is a method provided by PHP for storing user session information. When a user visits a website, PHP will assign each user a unique Session ID and store some data through this ID. This data can remain unchanged throughout the user's session until the user exits the session or closes the browser.
The use of Session is very simple. We just need to start the session through session_start() , and then use the $_SESSION array to store and get the data.
<?php
// Start a session
session_start();
// Store data to Session
$_SESSION['user_id'] = 1;
$_SESSION['user_name'] = "John Doe";
// Get data
echo $_SESSION['user_name']; // Output: John Doe
?>
Next, we will combine the mysqli_result function and Session storage to implement a function: dynamically obtain user data from the database and display it, while maintaining the persistent storage of user data after the page is refreshed or the user leaves.
First, we need to query the required data from the database and store this data into the Session . Here we assume that we want to obtain the user's basic information and store it in the Session so that it can be used directly in subsequent page access.
<?php
// Start a session
session_start();
// Connect todatabase
$mysqli = new mysqli("localhost", "user", "password", "database");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Execute a query
$query = "SELECT id, name, email FROM users WHERE id = 1";
$result = $mysqli->query($query);
// 将查询result存储到 Session
if ($result->num_rows > 0) {
$user_data = $result->fetch_assoc();
$_SESSION['user_data'] = $user_data;
} else {
echo "No user data found";
}
$mysqli->close();
?>
Next, we need to dynamically display the data stored in the Session on the page. No matter how many times the user refreshes the page, the data can be kept in the $_SESSION array as long as the Session has not expired or is cleared.
<?php
// Start a session
session_start();
// Check whether user data has been stored
if (isset($_SESSION['user_data'])) {
$user_data = $_SESSION['user_data'];
echo "user ID: " . $user_data['id'] . "<br>";
echo "user名: " . $user_data['name'] . "<br>";
echo "e-mail: " . $user_data['email'] . "<br>";
} else {
echo "No user data found";
}
?>
Sometimes, we need to clear the data in the Session when the user logs out or logs out. Session data can be cleared using session_unset() or session_destroy() .
<?php
// Start a session
session_start();
// Clear Session data
session_unset();
// destroy Session
session_destroy();
echo "You have successfully logged out";
?>
By combining the mysqli_result function and PHP Session storage, we can dynamically display the data in the database, and still maintain the data persistence after the user refreshes the page. Session provides a persistent storage space for each user, and mysqli_result allows us to easily get and process data from the database.
This technology is very useful for applications that require user login status, personalized settings, or temporarily store user data. By flexibly combining these two, you can improve user experience and effectively manage data.