PHP 101

From ESE205 Wiki
Jump to: navigation, search

Grocery Guru

Description

In order to establish a connection between an application and the SQL database, a server must be used. For this tutorial, we used Bluehost to create a web domain, a database, and a database user. With this information, we can write PHP files to establish a connection to the database and provide the ability to update or change the items in the table of the database.

Setting Up the Database

Create a new database on Bluehost and set up a user to access the database.

Databaselogin.png

Use phpMyAdmin, the built-in app on Bluehost, to create a table in the database. Set columns and insert rows into the database.

Table1.png

Creating a PHP File to Get An Item

Use the file manager in Bluehost to create a new PHP file in the public-html folder. Name the file with .php at the end. After creating the file, click to highlight it and use the code editor in the tool bar at the top to start writing code. To view the exact code we used, visit https://github.com/maggiearcher/GroceryGuru/blob/patch-1/service.php.

The above code establishes a connection with the database, creates an array of each row in the table, and displays them on your domain with the directory /service.php. The first line uses the hostname, database username, password, and database name. In your case, change "service" to the name of your file. The webpage is parsed as a JSON file in Xcode to search the database for a specific UPC number or variable in the table.

Service.png

Creating a PHP File to Edit a Row of the Table

Follow the same instructions as before to create a new PHP file in the public-html folder. This file will be used to edit certain values of a row in the table using an SQL query. Visit this webpage to view the code: https://github.com/maggiearcher/GroceryGuru/blob/patch-1/combine.php

Testing the PHP Files

In order to check whether the code in your PHP files has errors, use the Chrome applet Postman. Try to send a post request to change the values of a row. The echo statements in the PHP files should indicate where your code is failing.