What are the best practices for creating a database structure with multiple relationships in PHP?

When creating a database structure with multiple relationships in PHP, it is important to properly design the tables and establish the relationships between them using foreign keys. This helps maintain data integrity and ensures that the relationships between entities are accurately represented in the database.

// Example of creating a MySQL database structure with multiple relationships in PHP

// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Create tables with relationships
$sql = "CREATE TABLE users (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(30) NOT NULL
)";

$conn->query($sql);

$sql = "CREATE TABLE posts (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50) NOT NULL,
    content TEXT,
    user_id INT(6),
    FOREIGN KEY (user_id) REFERENCES users(id)
)";

$conn->query($sql);

$sql = "CREATE TABLE comments (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    content TEXT,
    post_id INT(6),
    user_id INT(6),
    FOREIGN KEY (post_id) REFERENCES posts(id),
    FOREIGN KEY (user_id) REFERENCES users(id)
)";

$conn->query($sql);

// Close the connection
$conn->close();