Developing a WeChat Public Account Personal Center with PHP
This article explains how to build a personal center for a WeChat public account using PHP, covering database table creation, retrieving user information via WeChat authorization, storing data, and rendering a user profile page with sample code snippets.
With the widespread use of WeChat, many enterprises and individuals are focusing on the WeChat public account platform. The personal center is a key feature for users. This guide shows how to develop a personal center for a WeChat public account using PHP, with concrete code examples.
Creating Database and Tables
First, create a user table in the database to store user information. An example table structure is shown below.
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
openid VARCHAR(50) NOT NULL,
nickname VARCHAR(50) NOT NULL,
avatar VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);Obtaining Basic WeChat User Information
In the personal center we need to display the user's avatar and nickname. This requires obtaining the user's basic information through WeChat authorization. The following PHP code demonstrates how to retrieve and store this data.
<?php
// Get user authorization
$userInfo = $wechat->getUserInfo($_GET['code']);
$openid = $userInfo['openid'];
$nickname = $userInfo['nickname'];
$avatar = $userInfo['headimgurl'];
// Save user information to database
$db->query("INSERT INTO users (openid, nickname, avatar) VALUES ('$openid', '$nickname', '$avatar')");Displaying the Personal Center Page
The page should show the stored user information and provide additional functions such as order history, favorites, and settings. The example below renders the avatar, nickname, and simple navigation links.
<?php
// Get user information
$user = $db->query("SELECT * FROM users WHERE openid = '$openid'")->fetch(PDO::FETCH_ASSOC);
// Display avatar and nickname
echo "<img src='{$user['avatar']}' alt='{$user['nickname']}' />";
echo "<h2>{$user['nickname']}</h2>";
// Display other personal center features
echo "<a href='#'>My Orders</a>";
echo "<a href='#'>My Favorites</a>";
echo "<a href='#'>Personal Settings</a>";By following these examples, a functional personal center can be built, allowing users to manage their information and access personalized services. Further extensions can be added according to specific requirements.
In summary, developing a WeChat public account personal center with PHP enables better user management and value‑added features, and the provided code serves as a practical starting point.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
