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 make sure that you are accessing a populated database. You can use example code to populate an example database.
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. Instruction on how to create a file can be found in Part 4: Upload, create, organize and name files using cPanel with user friendly interface. 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.