What are the potential pitfalls of using MySQL functions like mysql_query and mysql_fetch_row in PHP?

Using MySQL functions like mysql_query and mysql_fetch_row in PHP can lead to vulnerabilities such as SQL injection attacks and deprecated functionality. To mitigate these risks, it is recommended to use prepared statements with parameterized queries and switch to the improved MySQLi or PDO extensions.

// Connect to MySQL using PDO
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a statement with a parameterized query
$stmt = $pdo->prepare("SELECT * FROM mytable WHERE id = :id");

// Bind the parameter and execute the query
$stmt->bindParam(':id', $id);
$stmt->execute();

// Fetch the results as an associative array
$result = $stmt->fetch(PDO::FETCH_ASSOC);