In this post, I will share about how to make a simple access from PHP page to MySQL database using XAMPP. There are three main steps for beginners: XAMPP installation, creating database using phpMyAdmin, and accessing database using PHP. XAMPP version that I use is 1.7.3, so it is perhaps different from current version.
- Install XAMPP.
- After the installation finishes, the command prompt will appear and ask some options. I usually choose the default answers.
- Start XAMPP Control Panel.
- Make sure that Apache and MySql statuses are running.
- If they aren’t, it’s possible that there is another application using port 80, such as IIS or Skype. If you don’t want to change the using of port 80 for the application, you can change the Apache port in C:\xampp\apache\conf\httpd.conf (for example, replacing “Listen 80” with “Listen 85” in the file will change Apache port to 85). Consequently, you have to include the port number in browser address, for example: http://locahost:85/.
- Click the two Admin buttons at Apache and MySql the make sure that you can see these two pages on your browser.
- The working directory of web pages that will be created is located in C:\xampp\htdocs. First picture above shows that http://localhost/xampp/splash.php located in C:\xampp\htdocs\xampp\splash.php.
Creating Database using phpMyAdmin
- Create new database. Fill the name of your database in the text box as shown below (for example: database “school”), and then click Create.
- Create new table. Fill the name of your new table in the text box as shown below (for example: table “students” which has three fields), and then click Go.
- On the next page, fill the three fields in the text boxes, and then click Save below the table.
- This time, the text boxes that should be filled are Field (field name of the table), Type (field type), Length/Values (string size of the field), Null (the field may be null), Index: PRIMARY (setting primary key), and AUTO_INCREMENT (the field will be automatically filled by server using auto-increment number).
- The field can be edited by clicking the menu in column Action or below the table in the picture below.
- To add new content to the table, click Insert tab, fill the fields, then click Go.
- The result can be viewed by clicking Browse tab.
- You can create other tables by entering the database page which can be accessed at navigation menu located at the left of the web page.
- If you want, you can create relation design. Enter the database page. Click “Create Relation” button, then select referenced key and foreign key.
- You can get the exported SQL file. Click Export tab in database page, change the configuration needed, then click Go.
- SQL file can be imported. Click Import tab in database page.
Accessing Database using PHP
- Download file index.php (http://cid-38a55214b54a920a.office.live.com/self.aspx/.Public/Blog%20-%20XAMPP/index.php) and add.php (http://cid-38a55214b54a920a.office.live.com/self.aspx/.Public/Blog%20-%20XAMPP/add.php).
- Check the comments in the files if you want to get the explanation.
- Copy the files to a new folder in htdocs folder, for example: C:\xampp\htdocs\test.
- Open browser and go to the folder address, for example: http://localhost/test/.
Voilà! You can show tables and add row of a table on a web page. Hopefully this is useful for beginners.