Home > Backend Development > PHP Tutorial > Solution to the problem that hobbies cannot be displayed in PHP database

Solution to the problem that hobbies cannot be displayed in PHP database

WBOY
Release: 2024-02-29 21:10:01
Original
1087 people have browsed it

Solution to the problem that hobbies cannot be displayed in PHP database

In PHP development, we often encounter situations where we need to obtain the user's hobby information from the database and display it on the web page. However, sometimes the user's preferences may contain some special characters or contain multiple options, which prevents them from being displayed directly on the page. In this article, a solution is presented, using a concrete code example to demonstrate how to handle this situation.

First, assume that we have a table named "users", which contains the user's basic information and a field "hobbies" to store the user's hobby information. Our goal is to obtain the user's hobby information from the database and display it on the web page.

  1. Create database connection

First, we need to establish a connection with the database, which can be achieved using methods in PHP extensions such as PDO or mysqli. The following is a sample code for using PDO to connect to the database:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>
Copy after login
  1. Get user information from the database

Next, we need to write SQL statements to query the user's information from the database information, including its hobby field. After the query is completed, the results are stored in an array for subsequent processing. The following is a sample code for a simple query and storing the results in an array:

<?php
$stmt = $conn->prepare("SELECT id, name, hobbies FROM users");
$stmt->execute();

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
Copy after login
  1. Processing Hobby Information

When we get the user's hobby information from the database Finally, you may encounter some special characters or multiple options. In order to display this information correctly on the page, we need to perform processing, such as escaping special characters, or splitting multiple options and displaying them in the form of a list.

The following is a simple code example for processing user preference information, in which special characters are escaped and multiple options are displayed in the form of a list:

<?php
foreach($result as $row) {
    $hobbies = explode(',', $row['hobbies']);

    echo "<p><strong>{$row['name']}'s Hobbies:</strong></p>";
    echo "<ul>";
    foreach($hobbies as $hobby) {
        echo "<li>" . htmlspecialchars($hobby) . "</li>";
    }
    echo "</ul>";
}
?>
Copy after login

Through the above code, we The user preference information obtained from the database was successfully processed and displayed in the form of a list on the page. This ensures that no matter what special characters or multiple options are included in the user's hobby information, it will be displayed correctly on the page.

To sum up, through the above solutions and specific code examples, we can solve the problem that hobbies cannot be displayed in the database during PHP development, ensure that the user's information can be accurately presented on the page, and improve user experience.

The above is the detailed content of Solution to the problem that hobbies cannot be displayed in PHP database. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template