What alternative methods can be used to store related data in separate tables in PHP?

When dealing with related data in PHP, one alternative method to storing related data in separate tables is to use a relational database management system like MySQL. By creating multiple tables with relationships defined between them, you can store related data efficiently and ensure data integrity. This allows for better organization and retrieval of data when working with complex relationships.

// Example of creating two tables with a relationship between them in MySQL

// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Create a table for storing users
mysqli_query($connection, "CREATE TABLE users (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(30) NOT NULL,
    email VARCHAR(50) NOT NULL
)");

// Create a table for storing user posts with a foreign key relationship to the users table
mysqli_query($connection, "CREATE TABLE posts (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50) NOT NULL,
    content TEXT,
    user_id INT(6) UNSIGNED,
    FOREIGN KEY (user_id) REFERENCES users(id)
)");