If you access the database DIRECTLY through phpMyAdmin there no need to connect to it. However, when using a PHP script you have to CONNECT to the database INDIRECTLY. PHP can connect to most databases (e.g., PostgreSQL, SQLite, Sybase) servers. If you use a database that does not have DIRECT support, you will need to use a PHP Open Database Connectivity (ODBC) method with ODBC drivers to connect to a database. We will be using the MySQL server that comes bundle with XAMPP.
Once you create a database and its tables, phpMyAdmin will create a folder with the same name as the database in a server folder. In our case, it is C:/xampp/mysql/data/pdo_employee_directory1234 with database related files in it. It is important to remember that data for an application get saved in one folder (e.g., C:/xampp/mysql/data/) while the application get saved in another folder (e.g., C:/xampp/htdocs/). This is true for all applications that you build.
Before you can connect to a database, you have to first setup a testing or remote server. The process is more involved than creating a static website:
First, we need to setup a LOCAL static web site for our application. This is no different from any other static web site setup that you create in Dreamweaver.
When creating a database driven website you not only need to setup a LOCAL static web site in Dreamweaver but you also need to setup either a TESTING or REMOTE server. For this tutorial, we will be using a testing server.
CAUTION: It is important to note that the slashes in the path for the Server Folder can be
FORWORD OR BACK SLASHES ( \ ) depending on the OS:
- Mac/UNIX - Forward slash
- Windows - Back slash
Whereas, the slashes for the Web URL MUST ALWAYS BE FORWARD SLASHES ( / )
Once you have a site and server setup, then you can connect to a database. We have a standard PHP script that is provided for you. All you have to do is change the database credentials.
<?php $host ="localhost";
$username = "root";
$password = "";
$database = "pdo_employee_directory1234";
// Create Connection ----------------------------------------
$pdo_conn = new PDO( "mysql:host=$host; dbname=$database", $username, $password); ?>
CAUTION: When you don't have any credential, you will see the following message at the bottom of the phpMyAdmin screen, "Your configuration file contains settings (root with no password) that correspond to the default MySQL privileged account. Your MySQL server is running with this default, is open to intrusion, and you really should fix this security hole by setting a password for user 'root'."
<?php $host ="localhost";
$username = "Cornelius";
$password = "admin";
$database = "pdo_employee_directory1234";
// Create Connection ----------------------------------------
$pdo_conn = new PDO("mysql:host=$host; dbname=$database", $username, $password); ?>
CODE EXPLNATION:
- If you are working locally, the host will always be localhost, if you have an ISP, you need to provide the path
to your ISP server.
- mysql represents the database driver that is being used.
- $pdo_conn represents the database handler.
- 'mysql:host=localhost;dbname=pdo_employee_directory1234' -- notice the equal sign after the word host and
the semicolon between localhost and dbname with the whole statement enclosed in a set of single quotes.