Skip to content
PHP Logo

What are the potential issues with using deprecated mysql_* functions in PHP and what alternatives should be considered?

Using deprecated mysql_* functions in PHP poses security risks as they are no longer maintained and may expose your application to SQL injection attacks. It is recommended to switch to MySQLi or PDO extensions which offer more secure and modern ways to interact with databases.

php
// Using MySQLi extension to connect to a database
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');

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

// Perform a query using prepared statements
$stmt = $mysqli->prepare("SELECT * FROM table_name WHERE id = ?");
$stmt->bind_param("i", $id);
$id = 1;
$stmt->execute();
$result = $stmt->get_result();

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

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