What are some best practices for storing user data in a PHP and MySQL database for optimal performance and scalability?

When storing user data in a PHP and MySQL database for optimal performance and scalability, it is important to normalize the database structure, use indexes on frequently queried columns, and sanitize user input to prevent SQL injection attacks. Additionally, utilizing prepared statements can help improve performance by reducing the number of queries sent to the database.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Prepare SQL statement with placeholders for user input
$stmt = $mysqli->prepare("INSERT INTO users (username, email) VALUES (?, ?)");

// Bind parameters and execute the statement
$stmt->bind_param("ss", $username, $email);
$username = "john_doe";
$email = "john.doe@example.com";
$stmt->execute();

// Close the statement and database connection
$stmt->close();
$mysqli->close();