How can developers ensure their PHP scripts are compatible with PHP 5.5 and above, considering the removal of mysql_* functions?
Developers can ensure their PHP scripts are compatible with PHP 5.5 and above by replacing the deprecated mysql_* functions with mysqli or PDO functions for database operations. This involves updating the code to use prepared statements and parameter binding to prevent SQL injection vulnerabilities.
// 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 users WHERE username = ?");
$stmt->bind_param("s", $username);
$username = "john_doe";
$stmt->execute();
$result = $stmt->get_result();
// Fetch results
while ($row = $result->fetch_assoc()) {
echo $row['username'] . "<br>";
}
// Close the statement and connection
$stmt->close();
$mysqli->close();