Here I am going to show how can we make our php mysql connection more secure and easy to edit while move in production usin .ini

1.Create a file named: my_setting.ini

write below content in this file start.

 

[database]

driver = mysql
host = localhost
;port = 3306
schema = test
username = root
password = root

 

write below content in this file close.

now create a php file named: pdo.php
write below content in this file start.

 

<?php
ini_set("display_errors", 1);
echo "Hi";
 
class MyPDO extends PDO
{
    public function __construct($file = 'my_setting.ini')
    {echo "Hi2";

        if (!$settings = parse_ini_file($file, TRUE)) throw new exception('Unable to open ' . $file . '.');
      
        $dns = $settings['database']['driver'] .
        ':host=' . $settings['database']['host'] .
        ((!empty($settings['database']['port'])) ? (';port=' . $settings['database']['port']) : '') .
        ';dbname=' . $settings['database']['schema'];
      
        parent::__construct($dns, $settings['database']['username'], $settings['database']['password']);
    }
}
$x = new MyPDO;
?>

 

write below content in this file close.

Now run your file pdo.php it will work properly.

There is no need to change any setting regarding this.

Share on Facebook