How to connect PHP to MySQL using WAMP

Written by stephen byron cooper| 13/05/2017

WAMP includes a MySQL server. (WAMP)

WAMP stands for Windows, Apache, MySQL and PHP. It is a free bundle of applications that enables a Web server to run on a Windows PC. PHP is a programming language in which many Web pages are written. Apache is a Web server, which means it handles access to Web pages by process incoming requests. MySQL is a relational database system. Installing WAMP gives anyone the ability to test newly developed Web pages locally before uploading them to a remote server.

Create a database

Double click on the WAMP Server icon that was installed on your desktop during installation. This will start the server running. You will see a red W in your services tray at the bottom of your screen turn from red to green.

Click on the green W in the services tray. Select phpMyAdmin from the pop-up menu. In the application click the “Databases” tab at the top of the screen. For this example, we'll use a database of dogs; your categories will reflect the database you want to create. Type the name “dogs” in the “Create new database” field and the click the “Create” button.

In the next screen, enter “breeds” in the “Create new table” “Name field and enter “3” (without the quotes in both cases). Press the Go button. A new screen appears enabling you to enter details of each field in the table.

Enter “dog_id” in the “Field” box in the first row. Give it a “Type” of “INT” and a “Length” of “20.” Select “PRIMARY” from the ”Index” pick list and check the “A_I” box. Name the “Field” in the next row “dog_name,” give it a “Type” of “VARCHAR” and a “Length” of “100.” Call the third field “breed” and make that “VARCHAR” with a length of “100.” Click the “Save” button at the bottom of the screen.

Press the “Insert” tab at the top of the screen and fill in a record. Leave the dog_id field blank. Enter “Fluffy” for dog name and “Maltese” for breed. Press the “Go” button to save the record.

Create PHP file

Locate the folder called c:\wamp\www on your C drive. This folder was created during the installation of WAMP. Create a folder beneath this to hold all the files of your current Web development project. Move two files from the www folder to your new folder. These files are called index.php and testmysql.php.

Open your Web development tool. Create a new site in the developer and specify you want to work with PHP and MySQL. You should also specify that you will be editing and testing locally. Set the default directory for the site to the directory you created under c:\wamp\www.

Create a Database connection in your developer and specify a MySQL connection. Call it con_dogs, set the MySQL server to localhost and write “root” in the “Username” field. Leave password blank and select the database name from a list of databases in your local MySQL system. Select “dogs“ as the database.

Create a form with two fields, “Dog Name” and “Breed.” Add a “Submit” button and a “Cancel” button.

Add Server Behaviors to the form. Specify the con_dogs connection to enable the developer tool to connect to the database and fetch table details. Link the “Dog Name” field to the “dog_name” column in the table. Link the “Breed” form field to the “breed” column in the table.

Save the program and run it from the developer tool. Use the form to insert a record into the table. Go back to the WAMP server to see the new record in the table.