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

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

WBOY
WBOYOriginal
2024-02-29 16:03:03524browse

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

Solution to the inability to display hobbies in the PHP database

When developing a website or application, it often involves operations that interact with the database . Sometimes we need to display the user's hobbies information on the page, but storing the user's hobbies in the database is not a simple matter. This article will introduce how to solve the problem that the database cannot display hobbies in PHP, and provide specific code examples for reference.

Problem Description

Suppose we have a user table (users) and a hobby table (hobbies). The user table stores the user's basic information, and the hobbies table stores the user's various hobbies information. , the two tables are related through user ID. Now we need to display the user's hobbies information on the page based on the user ID, but the user's hobbies cannot be directly displayed in the database.

Solution ideas

In order to solve this problem, we can implement it through the following steps:

  1. Establish an association table of users and hobbies (user_hobbies) in the database, The user ID and corresponding hobby ID are recorded in the table.
  2. Query the user's information in PHP and query the corresponding hobby ID through the user ID.
  3. Query specific hobby information based on hobby ID.
  4. Display the user's hobby information on the page.

Specific implementation

1. Create an association table

First, create an association table named user_hobbies in the database for Stores user and hobby related information. The table structure is as follows:

CREATE TABLE user_hobbies (
    id INT(11) AUTO_INCREMENT PRIMARY KEY,
    user_id INT(11),
    hobby_id INT(11)
);

2. Query user information

In the PHP code, we first query the user's basic information and obtain the user ID. Assume that we have obtained the user ID 1.

<?php
$userId = 1;
// 查询用户信息的SQL语句
$query = "SELECT * FROM users WHERE id = $userId";
// 执行查询操作
// 这里假设使用PDO进行数据库操作
$stmt = $pdo->query($query);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
?>

3. Query the user’s hobby information

Next, we query the user’s hobby information based on the user ID.

<?php
// 查询用户的爱好ID的SQL语句
$query = "SELECT hobby_id FROM user_hobbies WHERE user_id = $userId";
$stmt = $pdo->query($query);
$hobbyIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
?>

4. Query the hobby information and display it

Finally, query the specific hobby information based on the obtained hobby ID and display it on the page.

<?php
// 根据爱好ID查询爱好信息的SQL语句
$query = "SELECT * FROM hobbies WHERE id IN (".implode(',', $hobbyIds).")";
$stmt = $pdo->query($query);
$hobbies = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 在页面上展示用户的爱好信息
foreach ($hobbies as $hobby) {
    echo $hobby['name']."<br>";
}
?>

Through the above steps, we successfully solved the problem of being unable to display user hobby information in the PHP database. By establishing a correlation table, querying the correlation information between users and hobbies, and displaying specific hobby information, we can display the user's hobbies information on the page.

Conclusion

The inability to directly display hobby information in the database is a common problem, but through reasonable design and query operations, we can easily solve this problem. I hope the solutions described in this article are helpful to you. If you have any questions or suggestions, please feel free to leave a message to communicate.

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

Statement:
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