Before using database we need to connect it first. This process is common in all types of database irrespective of language we use. We will learn here the code for connecting to MySQL database using PDO class.
PHP PDO Basics: Installation, support & Connection
It is always a good practice to keep the database connection details at a common place and call it from different pages where it is required. This helps in maintaining the code when the database connection details changes or the script is shifted to different server with different connection details. If we are not using a common file then to change the database details we need not change the connection string in all the files. By keeping all details at one place if we update the details then all the files will use the same ( updated ) details.
We can keep the connection details in config.php file or any other file name you find suitable. All the codes inside this file are to be within the PHP code block, so it will not be exposed if it is opened directly in browser.