How can one effectively transition from using the deprecated mysql* API to MySQLi or PDO in PHP scripts?

To effectively transition from using the deprecated mysql* API to MySQLi or PDO in PHP scripts, you can start by replacing all occurrences of mysql_* functions with their MySQLi equivalents or by rewriting the code to use PDO. This will ensure compatibility with newer versions of PHP and improve the security of your application by utilizing prepared statements to prevent SQL injection attacks.

// Before transitioning from mysql* API to MySQLi or PDO
$connection = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database', $connection);
$result = mysql_query('SELECT * FROM table', $connection);
while ($row = mysql_fetch_assoc($result)) {
    // Process data
}

// After transitioning to MySQLi
$connection = new mysqli('localhost', 'username', 'password', 'database');
$result = $connection->query('SELECT * FROM table');
while ($row = $result->fetch_assoc()) {
    // Process data
}

// After transitioning to PDO
$connection = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
$statement = $connection->query('SELECT * FROM table');
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
    // Process data
}