In the past, I’ve always created a database class and in that class assigned a $connection attribute the connection via mysql_connect in a __construct method. In the same file, I would instantiate the class so that it was ready to go. Then whenever I needed that connection I would simply require that database file and add a global $connection in the method that need the $connection. What is the best way to achieve something similar or better using php’s PDO?
Share
I believe that there’s no “Best” way to achieve what you ask but here is what I use. The function execute was designed according to my needs, you may change it however you want.
** EDIT **
By the way, I am using a singleton method in this class since I call the class several times from different files. Therefore, you may change that as well.