Skip to content
PHP Logo

How can the use of deprecated PHP functions like mysql_ be updated to adhere to modern coding standards?

The use of deprecated PHP functions like mysql_ should be updated by replacing them with the modern mysqli or PDO extensions. This ensures compatibility with newer PHP versions and adherence to current coding standards.

php
// Deprecated way using mysql_
$connection = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database_name', $connection);
$result = mysql_query('SELECT * FROM table', $connection);

// Updated way using mysqli
$connection = mysqli_connect('localhost', 'username', 'password', 'database_name');
$result = mysqli_query($connection, 'SELECT * FROM table');

// Updated way using PDO
$connection = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
$statement = $connection->query('SELECT * FROM table');