Thursday 23 February 2017

How to connect to a database via PDO with PHP

To use your PHP PDO must have version 5.0 and up. Here Techfree will show you how to connect to the database via PDO with PHP:

1. Open your Web Server example Warm, Xampp, Mamp (for Mac OS)


2. Create a PHP file. Examples connection.php you need to know what your Hostname? , Name and password for access to the database? And want to connect to the database?

See the example below:
+ $ Hostname = 'localhost'; Hostname is the name. My hostname localhost.

+ $ Username = 'root'; Is the name used to access the database phpMyAdmin. The root name for my access to the database.

+ $ Password = "; password is blank, meaning no password.

+ $ Dbname = 'techfree'; Is the name of the database you want to connect to. techfree my name Database.

In column 9 of the code is the code for connecting to the PDO class to the database.

Among the codes in the range of 10 means throwing error of the PDO to the exception.

Range 11 is open for display on the website (browser) when you connect to the database successfully.

Range 13 $ dbh = null; Kim to disconnect the connection.

Visit the website (browser) and go to the location where you saved connection.php If you saw the result as the picture below, you can connect to the database was successful.



0 comments: