What are the potential pitfalls of using outdated PHP functions like mysql_connect and mysql_query?

Using outdated PHP functions like mysql_connect and mysql_query can pose security risks as they are deprecated and no longer supported in newer versions of PHP. This can leave your application vulnerable to SQL injection attacks and other security threats. To solve this issue, it is recommended to use modern alternatives like PDO or MySQLi for database connections and queries.

// Using PDO for database connection and query
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare("SELECT * FROM table");
    $stmt->execute();

    // Fetch results
    $results = $stmt->fetchAll();

    // Process results
    foreach ($results as $row) {
        // Do something with each row
    }

} catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}