If you want to see the exception that may occur, you can change the parameters in the database.ini file to an invalid one and test it. A connection to the PostgreSQL database sever has been established successfully. Generating optimized autoload files Code language: SQL (Structured Query Language) ( sql )įinally, launch the index.php file from the web browser to test it. Run the following composer command to update the autoload files: >composer dump-autoload -o PHP throws a \PDOException if there is an exception occurs when connecting to the PostgreSQL database server, therefore, you need to place the code of creating a new PDO object inside the try.catch block to handle the exception. connect() Įcho 'A connection to the PostgreSQL database sever has been established successfully.' In the connect() method, we read the database configuration parameters in the database.ini file, construct a connection string and pass it to the PDO constructor.Īfter that, place the following code in the index.php file. To connect to a PostgreSQL database, you need to create a new instance of the PDO class.If the instance already exists and you try to create a new one, the class will return the existing reference. It means that you can create only one instance for the class. The Connection class is a singleton class.* return an instance of the Connection object setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION) Then, create a new class called Connection in the Connection.php file. Password=postgres Code language: SQL (Structured Query Language) ( sql ) Next, use the database.ini file to store the PostgreSQL database parameters as follows: host=localhost CREATE DATABASE stocks Code language: SQL (Structured Query Language) ( sql ) The project structure will look like the following picture: Connect to the PostgreSQL databaseįirst, create a new database named stocks for the demonstration. Generating autoload files Code language: JavaScript ( javascript )Īfter that, create the index.php file in the postgresqlphpconnect folder.įinally, create two more files in the app folder: Connection.php and database.ini. Updating dependencies (including require-dev) Loading composer repositories with package information Because we didn’t declare any library in the composer.json file, it just generates the autoload file. The command will also place all third-party libraries in the newly created vendor folder. This command instructs the Composer to download the declared libraries in the composer.json file and generate an autoload file. Then, go to the window terminal, navigate to the postgresqlphpconnect folder, and type the following command: >composer update It means that every class that you create in the app folder will map to the PostgreSQLTutorial namespace. Next, create the app folder and a new composer.json file in the postgresqlphpconnect folder with the following content: Code language: SQL (Structured Query Language) ( sql ) We will use the Composer to set up the project structure of all the projects that we will be working on.įirst, create the postgresqlphpconnect folder in the webroot folder to store the project files. The Composer is a tool for managing dependency that allows you to declare the PHP library in a project and manage the update automatically. extension=php_pdo_pgsql.dll Code language: SQL (Structured Query Language) ( sql ) Create a PHP project structure with Composer To uncomment the line, you remove the semicolon ( ) at the beginning of the line and restart the webserver. However, if this is not the case, you can enable the extension by editing the php.ini file to uncomment the following line: extension=php_pdo_pgsql.dll Code language: Shell Session ( shell ) Most PHP distributions include the PostgreSQL extension PDO_PGSQL by default so you don’t need to do any further configuration in PHP. Summary: in this tutorial, you will learn how to setup a simple project structure and connect to the PostgreSQL database using PHP PDO API.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |