Are there any potential security risks in the PHP code shown, especially in terms of database connectivity and querying?

The code snippet provided is vulnerable to SQL injection attacks as it directly inserts user input into the SQL query without sanitization. To mitigate this risk, you should use prepared statements with parameterized queries to prevent malicious input from affecting the database operations.

// Establish database connection
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Prepare a SQL query using a parameterized statement
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

// Set the parameter and execute the query
$username = $_POST['username'];
$stmt->execute();

// Fetch results
$result = $stmt->get_result();

// Process the results as needed

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