PHP : Migration from Mysql Extension to PDO

Surely when we first learn PHP programming most of us learn the mysql_*** functions (mysql_connect(), mysql_query(), mysql_fetch_array(), etc.) to interact with MySql databases.

Get ready to learn new ways to perform database operations because the PHP development team said it will remove the mysql extension (mysql_***) functions in future PHP releases. Based on this link the development team will indeed remove the mysql extension function but not suddenly.

Greetings PHP geeks,

Don’t panic! This is not a proposal to add errors or remove this popular extension. Not yet anyway, because it’s too popular to do that now.

The documentation team is discussing the database security situation, and educating users to move away from the commonly used ext/mysql extension is part of this.

Based on the announcement, PHP developers are also advised to use 2 alternatives to the mysql extension, namely pdo_mysql and mysqli.

Well, if you don’t believe me, just read it yourself at the link above 😀

If you’ve read it means it’s time to learn to use PDO to interact with any database, not just MySQL. How? much cooler right?

PHP PDO

Connection to Database
This PDO supports several popular databases, not only mysql as supported by the mysql_*** function. Some of the databases supported by PDO include:

MySql
firebird
IBM DB2
PostgreSql
Sql Lite
To connect to the MySql database, we use the following method

<?php

/*** mysql hostname ***/
$hostname = ‘localhost’;

/*** mysql username ***/
$username = ‘username’;

/*** mysql password ***/
$password = ‘password’;

/*** mysql database / $database= ‘Databasename’; try { $dbh = new PDO(“mysql:host=$hostname;dbname=$database”, $username, $password); / print a message if the database has been successfully connected ***/
echo ‘Connected to database’;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Judging from the syntax above, it can be seen that PDO uses the concept of OOP. Well .. because PHP in the future does lead to the path of OOP then it should be started to learn object-oriented programming paradigm :D.

Every time we have opened a connection to a database, we are obliged to close it when it is no longer used. To close it we simply assign a null value to the connection object

<?php

/*** mysql hostname ***/
$hostname = ‘localhost’;

/*** mysql username ***/
$username = ‘username’;

/*** mysql password ***/
$password = ‘password’;

/*** mysql database / $database= ‘Databasename’; try { $connection = new PDO(“mysql:host=$hostname;dbname=$database”, $username,$password); / print a message if the database has been successfully connected ***/
echo ‘Connected to database’;

/*** Close connection ***/
$connection=null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Insert Query
To enter data into the database, of course, we use an insert query. How to use it on PDO?, see the following example. I use Prepared Statements because this technique is safe from sql injection interference besides that the prepared statement objects are compiled first so that the next call will be fast.

Conclusion
How did your friends respond after getting to know PDO and how to use it as well as OOP techniques?

Of course from now on get used to using OOP techniques in programming.
Of course also start leaving the mysql extension function because in the future it will be removed

Hopefully this article is useful