What potential issues can arise from using outdated PHP functions like mysql_query in a script?
Using outdated PHP functions like mysql_query can lead to security vulnerabilities, as these functions are deprecated and no longer supported. To solve this issue, you should update your code to use modern database extensions like MySQLi or PDO, which provide better security features and support prepared statements to prevent SQL injection attacks.
// Connect to the database using MySQLi
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Perform a query using prepared statements
$stmt = $mysqli->prepare("SELECT * FROM table WHERE column = ?");
$stmt->bind_param("s", $value);
$stmt->execute();
$result = $stmt->get_result();
// Fetch results
while ($row = $result->fetch_assoc()) {
// Process the data
}
// Close the statement and connection
$stmt->close();
$mysqli->close();