Using PHP to query a MySQL Database

This page contains instruction on how to query a cPanel database and show the results on your webpage using PHP.

Set up testing environment

The iSchool Help Desk uses the following resources to create this instruction:

  1. MySQL Workbench in your local computer

  2. iSchool cPanel

  3. 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.

Step-by-step guide

  1. 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.

  2. 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. 

  3. 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. 



Keywords"PHP query", PHP, "using PHP", MySQL, "MySQL Database", "How to", cPanel   Doc ID127452
OwnerMatthew M.GroupSchool of Information Sciences
Created2023-04-25 09:28:46Updated2023-07-28 12:55:57
SitesUniversity of Illinois School of Information Sciences
Feedback  0   0