What are some best practices for creating and managing MySQL tables in PHP scripts?

When creating and managing MySQL tables in PHP scripts, it is important to follow best practices to ensure efficiency and security. Some key practices include using parameterized queries to prevent SQL injection attacks, properly sanitizing user input, and organizing your database schema to optimize performance.

// Example of creating a MySQL table using PDO and parameterized queries
try {
    $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "CREATE TABLE IF NOT EXISTS users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(50) NOT NULL,
        email VARCHAR(50) NOT NULL
    )";

    $pdo->exec($sql);
    echo "Table created successfully";
} catch(PDOException $e) {
    echo "Error creating table: " . $e->getMessage();
}