Ecology lab



PHP and MySQL

How PHP and MySQL will work together

• the PHP script located anywhere on your server will communicate with the course given DB server

o as long as you have the given “CommonMethods.php” file

• the PHP script will request the data from the DB server

o PHP script will have MySQL code in it!!

• DB server will then respond with the data

• notice the web user NEVER interacts directly with the DB server

• CommonMethods.php contains the

o link to the DB server

o your username and password

|PHP and MySQL server setup |

|[pic] |

Pre-created CommonMethods.php file

• pre-created PHP file that has common methods you need

o saves coding

o saves time

o file can be included by OTHER PHP files

▪ that you will create

• the file placement

o needs to be SOMEWHERE in your www directory

o please only have one copy, so no inconsistencies

o other files can use relative path to link to this file

|Directory Setup |

|[pic] |

|relative URL paths |

|Link |meaning |

|1 |include('CommonMethods.php'); |this file is in the SAME directory as CommonMethods |

|2 |include('../CommonMethods.php'); |this file is one directory deeper than CommonMethods |

|3 |include('../../CommonMethods.php'); |this file is two directories deeper than CommonMethods |

|[pic] |

What do you do with CommonMethods File

• look at the file given

o notice there are some functions already created for you

▪ connect ( connects to the DB server given to you

▪ executeQuery ( just like it sounds

• YOU need to change

o Username and Password

▪ username shows up twice

• Do not share your version since it will have YOUR username and password

Dynamically entering data in the DB

• dynamic since the USER will be creating the data

o will have a form on the HTML page

• our PHP Script will accept and enter the data into the DB

• make sure your table is already in place!!

|Dynamically entering data in the DB |

|[pic] |

• Starting with the HTML Document

o needs a form with inputs to send to the PHP script

▪ list of input (HTML5!!)



o no “CommonMethods.php” needed since THIS document does NOT have direct access to the DB

▪ nor can it since it’s not a php page

|Example HTML to enter Dynamic Data |

|HTML Code |HTML Rendered |

| |[pic] |

| | |

|Lupoli's Little League Baseball Team Roster | |

| | |

| | |

| | |

|Lupoli's Little League Roster | |

| | |

| | |

| | |

| | |

|First Name: | |

|Last Name: | |

| | |

| | |

|Number: | |

|1 | |

|2 | |

|3 | |

|4 | |

|5 | |

|6 | |

|7 | |

|8 | |

|9 | |

|10 | |

|11 | |

|12 | |

|13 | |

|14 | |

|15 | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

• Accepting the dynamic data with the PHP page

o will need “CommonMethods.php” since we will dump data into DB

• will complete in stages

o vardump

▪ just to see that the data entered matched

▪ great tool to use

o db setup and data entry

▪ using “CommonMethods.php” and other code shown previously

▪ need MySQL setup

• make sure DB table is already in place

|Example DB Table |

|[pic] |

|Two Stages of the PHP Script |

|Stage 1 Var_dump |

| |

| |

|Stage 2 DB Entry |

| |

| ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download