Skip to content
PHP Logo

What potential security risks are present in the provided PHP code, especially in relation to the use of deprecated mysql functions?

The use of deprecated mysql functions in PHP code poses a significant security risk as these functions are no longer maintained and could potentially expose the application to SQL injection attacks. To address this issue, it is recommended to switch to MySQLi or PDO for database interactions as they offer better security features and prepared statements to prevent SQL injection.

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

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

// Query using prepared statement
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$username = "example";
$stmt->execute();
$result = $stmt->get_result();

// Fetch data
while ($row = $result->fetch_assoc()) {
    // Process data
}

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