Using PHP to query a MySQL Database
Set up testing environment
The iSchool Help Desk uses the following resources to create this instruction:
- MySQL Workbench in your local computer
- iSchool cPanel
- Populated Database. To access information from a database, you will need to ensure you are accessing a populated database. You can use example code to populate an example database.
Step-by-step guide
-
Create a MySQL database, user account, and password on cPanel. Instruction on how to create MySQL database can be found in the MySQL installation and configuration. After completing this step, you will have the MySQL Workbench database connected with the created cPanel database.
-
Create a file with the .php extension, under the public_html directory in your cPanel directory. Instructions on how to create a file can be found in Web Development Resources 101's guidance for uploading, creating, organizing, and naming files using cPanel. If you prefer a command-line interface, you can use touch command in the iSchool UNIX tutorial. Alternatively, you can build your PHP code in an editor, then upload it to that folder later.
-
Modify the created PHP file. Using the example PHP code as a foundation, change the server information to match the username you created, password, and database name. Additionally, change the query so it retrieves the information you are trying to access. Comments have been added throughout the PHP code to try to provide additional assistance and explanation. You can copy the code linked above and paste it into MySQL Workbench or PHPMyAdmin in cPanel. For the code to work properly, you will need to change the database and table name.