Home >Database >Mysql Tutorial >How Can I Migrate My Deprecated PHP mysql_* Functions to PDO?
Migrating Old PHP MySQL Code with Deprecated mysql_* Functions
Challenge:
As you've discovered, the mysql_* functions used in your PHP code are outdated and should be replaced with more secure and reliable alternatives. This article aims to guide you through the process of rewriting your code effectively.
Connection String Transition:
Originally, the connection information was managed through separate variables:
$db = new dbConn('127.0.0.1', 'root', 'pass', 'people', 'animals');
With PDO, the equivalent connection string resides in the constructor:
$db = new PDO('mysql:host=127.0.0.1;dbname=people;charset=UTF-8', 'root', 'pass');
The dbName parameter in the connection string represents the default database.
Database Selection:
In your previous code, database selection involved a selectDb() method:
$this->db->selectDb("people");
With PDO, database selection is not implemented in the same manner. However, you can set the default database using the constructor:
$db = new PDO('mysql:host=127.0.0.1;dbname=people;charset=UTF-8', 'root', 'pass');
Unnecessary Functions:
Conclusion:
By migrating your code to PDO, you enhance its security and stability while simplifying its implementation. The deprecated mysql_* functions are no longer required, and the connection handling is streamlined. This transition improves the reliability of your database interactions.
The above is the detailed content of How Can I Migrate My Deprecated PHP mysql_* Functions to PDO?. For more information, please follow other related articles on the PHP Chinese website!