Saturday, December 4, 2010

Examples of using PHP with a MySQL database

University of Virginia MySQL Self Service

Logging In

phpMyAdmin requires you to log in with a MySQL account name and password. All of the other options require you to log in with NetBadge (using your UVA computing id and password, or certificate).

Logging Out

Protect your identity and privacy. Completely exit your web browser when you are finished.

When you log in your web browser saves credentials that give you continued access without re-entering your password. If you leave your computer unattended then someone else can use it to access web services using your credentials without entering your user name and password. The phpMyAdmin log out link does not delete your saved MySQL password. You must still exit your browser.

Disk Quotas

The disk usage of a MySQL account is the total disk space occupied by all of its databases. Each MySQL account has a soft disk quota (default 50 Mbytes) and a hard disk quota (default 100 Mbytes). If the disk usage is over the soft quota then a weekly job sends an email notification to the contact address for the account. If the disk usage is over the hard quota then a nightly job suspends the account, which prevents logins. You can use the Un-Suspend MySQL Account option to un-suspend an account and clean up its databases.

You can use the Request Disk Quota Change option to request a larger disk quota.

Getting Started

If you do not have any MySQL accounts or databases on this server, then do the following:

  1. Create a MySQL account.
  2. Create a database.
  3. Create tables in your database with phpMyAdmin, or follow the installation instructions for your web site software package.

To access your databases, the MySQL server host name is dbm2.itc.virginia.edu. Log in with your MySQL account name and password.

Self Service Options

Administer Databases with phpMyAdmin

Log in to phpMyAdmin with a MySQL account name and password. Use phpMyAdmin to administer your existing databases. (See Create Database to create a new database.)

If you logged in to phpMyAdmin earlier in this browser session and you now want to log in using a different MySQL account name and password, then you should completely exit your browser first. Otherwise your browser may automatically log you in to phpMyAdmin using the previous MySQL account name and password.

MySQL Account and Database Status

Displays the status of your MySQL accounts and databases. Shows disk space used, disk quotas, and contact email addresses. Indicates if an account is suspended or if a database has public read enabled.

Create MySQL Account

Create a new MySQL Account. You may create a personal account for yourself or a group account for a MyGroups group. If using MyGroups, then the group name may not exceed 16 characters, and may not match a UVa Computing Id such as mst3k. If you have recently created a new MyGroups group, then wait at least half an hour for the group information to propagate to this server.

Change MySQL Account Password

Change the password for a MySQL account. Remember to update any web sites that log in to this MySQL account.

Change MySQL Account Email Address

Change the contact email address for a MySQL account. This email address receives notifications regarding the status of the account, such as if the disk quota is exceeded. You cannot change the contact email for a personal account. For a group account you may use either your own personal email address or the email address of the entire group. If a different member of the group should be the contact, then that person must set the email address.

Un-Suspend MySQL Account

If an account is over its hard disk quota, then a nightly job suspends it, which prevents logins. Use this option to un-suspend the account so that you can log in and clean up its databases.

Dropping (deleting) a database frees the disk space occupied by the database. Dropping or truncating a database table frees the disk space occupied by the table. By itself, deleting rows from a table does not free any disk space. To free disk space occupied by deleted rows, you can optimize the table with phpMyAdmin, or you can run the MySQL command: OPTIMIZE TABLE tablename (where tablename is the name of the table). If you need to increase your disk quota, then use the Request Disk Quota Change option.

Request Disk Quota Change

Send an email request to change the disk quota for a MySQL account. You may request up to 250 Mbytes at no charge. Beyond that you must lease enterprise-grade disk storage with backup.

Delete MySQL Account

You may not delete a MySQL account if it still has any databases. See the Drop Database option to delete the databases.

Create Database

Create a new MySQL database. The name of the database may not exceed 64 characters and it may contain only letters, digits, or underscores ('_'). (You cannot create databases with phpMyAdmin on this server.)

Drop Database

Drop (delete) a MySQL database. ITC retains backups of databases for two weeks after which time we cannot restore the database. We highly recommend that you use phpMyAdmin to create your own backup copy before dropping the database. (You cannot drop databases with phpMyAdmin on this server.)

Enable Database Public Read

Grant read only (select only) access on a database to the publicread MySQL account. No password is required to log in to publicread. Web sites that do not need to modify the database may access the database with the publicread account to protect the MySQL account and password of the database owner.

Disable Database Public Read

Revoke all access on a database from the publicread MySQL account.

Administrator Options

The following options are for administrators only.

Change Disk Quota

Change the disk quota for a MySQL account.

MySQL Account and Database Summary

Show a summary of all MySQL accounts and databases.

http://itc.virginia.edu/desktop/web/database/inventory/display/home.html

Inserting data into the database

Input form

Add a new computer

Minimal formatting

More Formatting



$hostName
= "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

mysql_connect($hostName,$userName,$password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

$query = "INSERT INTO computers (computerDescription) VALUES ('$computerDescription')";
$result = mysql_query($query);
print
"Data submitted to database!


Computer Description: $computerDescription
"
;

// Close the database connection
mysql_close();
?>

View the source


Computer Added


//include stylesheet for formatting
include("stylesheet.php3");
?>




// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

$query = "INSERT INTO computers (computerDescription) VALUES('$computerDescription')";
$result = mysql_query($query);

print
"Data submitted to database!


$computerDescription
has been added to the Computer List.
"
;

// Close the database connection
mysql_close();
?>


View the source of computers.php3



View the source of stylesheet.php3


Input form Add a new employee Minimal formatting More formatting



$hostName
= "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

mysql_connect($hostName,$userName,$password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

$query = "INSERT INTO employees (firstName, lastName) VALUES('$firstName', '$lastName')";
$result = mysql_query($query);
print
"Data submitted to database!


FirstName: $firstName

LastName: $lastName
"
;

// Close the database connection
mysql_close();
?>

View the source


Employee Added


//include stylesheet for formatting
include("stylesheet.php3");
?>





// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

$query = "INSERT INTO employees (firstName, lastName) VALUES('$firstName', '$lastName')";
$result = mysql_query($query);
PRINT
"Data submitted to database!


First Name: $firstName

Last Name: $lastName

";

// Close the database connection
mysql_close();
?>

View the source


Input form Add a new
inventory record
Minimal formatting More formatting

Add a new record to the inventory:

Date Acquired (yyyy-mm-dd):

Comments:

View the source of this page



// script to collect new records to be added to the Inventory table

// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

// Select all the fields in all the records of the Employees table
$employeeQuery = "SELECT *
FROM employees
ORDER

Add a record to the inventory

Date Acquired (yyyy-mm-dd):

Employee:

Computer:

Comments:

View the source of newInventory.php3

View the source of stylesheet.php3


Add an inventory record


//include stylesheet for formatting
include("stylesheet.php3");
?>




Add a record to the inventory





// script to collect new records to be added to the Inventory table

// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

// Select all the fields in all the records of the Employees table
$employeeQuery = "SELECT *
FROM employees
ORDER BY lastName, firstName"
;
$employeeResult = mysql_query($employeeQuery);

// Select all the fields in all the records of the Computers table
$computerQuery = "SELECT *
FROM computers
ORDER BY computerDescription"


print "


"
;

?>

PHP

PHP (Hypertext Preprocessor) is a simple scripting language that can be used to create dynamic Web pages. PHP scripts, which are embedded in HTML, are run on the server side, so in order for them to work, the Web server has to be configured appropriately.

The primary purpose of this document is to let you know that PHP is available on the Web servers www.people.virginia.edu, www.virginia.edu, faculty.virginia.edu, indorgs.virginia.edu, and www.itc.virginia.edu. If you would like to use PHP scripts in your personal Web site, you simply need to save a PHP file in your public_html directory with either of the following filename extensions:

  • .php
  • .phtml

Special note: The current version of PHP on www.people.virginia.edu, faculty.virginia.edu, indorgs.virginia.edu, and www.virginia.edu is PHP4. Please note that the .php3 filename extension is still valid, but your scripts will actually run using PHP 4.

See our document on PHP4 and Global Variables to learn what changes you may need to make to your php code:
http://www.itc.virginia.edu/desktop/web/php4.html

If you save the file with a .phps filename extension, you may view the source code (color-coded) from a Web browser.

ITC offers this service to those who have interest in using PHP, but little or no support is offered.

  • If you would like more information on PHP, we recommend you view the following tutorial (which offers links to other tutorials as well):

http://www.php.net/tut.php

  • For an example of using PHP for processing form input, please see:

http://www.itc.virginia.edu/desktop/web/phpform.html

  • For examples of using PHP with a MySQL database, please see:

http://www.itc.virginia.edu/desktop/database/php.html

  • For information about training in PHP and MySQL, please see the Department of Training web site:

http://www.web.virginia.edu/DOTWeb/itccourselist.cfm

Changes to PHP on ITC-maintained web servers

Overview

register_globals is a PHP setting that controls availability of variables that have been submitted by a user to a PHP script (such as data posted from a form, URL-encoded data, or data from cookies). In earlier releases of PHP, register_globals was set to "on", which made for easier, but less secure coding.

ITC is currently in the process of upgrading various web servers to PHP 4.2.1, which has register_globals set to "off". While coding in this environment is slightly more cumbersome, there are substantial security benefits. You may read more about this change in the PHP 4.1.0 Release Announcement.

If you have received notice that your web server is being upgraded and you currently maintain a site that contains PHP, you need to choose from the following two options:

  • To continue using global variables, you will need to follow the instructions for setting up a .htaccess file.
  • If you want to rewrite your code so that it does not rely on global variables (this is strongly encouraged for security reasons), please see the examples section.

If you do not implement one of the above options, your site will likely not work after the upgrade.


Setting up a .htaccess file

If you want to continue using global variables, you will need to create a .htaccess file in your site's main directory (or the main directory of your site that contains PHP scripts). Your .htaccess file should contain the following line:

php_flag register_globals on

We recommend that you create the .htaccess file by logging into your site with SecureCRT, Nifty Telnet, or another telnet application and using a Unix editor (such as pico, jove, vi, or emacs). Windows and Macintosh editors often insert special characters that may corrupt the .htaccess file and make your site unreachable.


Examples of PHP with register_globals set to "off"

The following examples demonstrate how to work with user-submitted data in an environment where register_globals is set to "off". The developers of PHP strongly encourage coding in this way. For more complete documentation, please see the PHP 4.1.0 Release Announcement.

Example of an html form processed by a PHP script

The form below is a plain html form that is processed by a file called register.php3 (in other words, the ACTION of the form is "register.php3"). Each field name in the form is unique and may be referenced as a php variable in register.php3. For example, the html field name for the First Name field is:

firstname

This may be referenced in register.php3 as:

$firstname

register.php3 performs three basic actions:

  • Displays text to the browser
  • E-mails the conference administrator the information supplied from the form
  • E-mails the person who filled out the form a confirmation message

Click here to view the source of register.php3

For more information about availability of php, please see:

http://www.itc.virginia.edu/desktop/web/php.html


Conference Registration:

First Name:

Last Name:

Full e-mail address:

Dinner selection:

Comments:




/* Script to process registration information from home.html */

/* Display text to the browser */
PRINT "
Registration for $firstname $lastname

Registration information for $firstname $lastname


Thank you for registering for the conference!


You will receive confirmation of your registration
via e-mail shortly.";

/* E-mail the conference administrator the information supplied in form */
mail("mpc3c@virginia.edu", "Registration for $firstname $lastname",
"First Name: \t $firstname
Last Name: \t $lastname
E-mail address: \t $email
Dinner selection: \t $dinner
Comments: \t $comments"
, "Reply-To: $email");

/* E-mail the person who filled out the form a confirmation message */
mail ("$email", "Registration Confirmation",
"Dear $firstname $lastname, \n
We have received your conference registration information
and have noted your meal preference of $dinner. Please
let us know if you have any questions or need further
assistance"
, "From:mpc3c@virginia.edu");

?>


View the source




http://itc.virginia.edu/desktop/web/database/inventory/display/home.html

Simple Queries

One-table query

List of computers

Minimal formatting

More formatting

One-table query

List of employees

Minimal formatting

More formatting

Multi-table query

Inventory

Minimal formatting

More formatting


Query based on user input

Pick from a list

Select an employee

Minimal formatting

More formatting

Drop down menu

Select an employee

Minimal formatting

More formatting

Text search

Enter an employee's name

Minimal formatting

More formatting


Inserting data into the database

Input form

Add a new computer

Minimal formatting

More Formatting

Input form

Add a new employee

Minimal formatting

More formatting

Input form

Add a new
inventory record

Minimal formatting

More formatting

One-table query

List of computers

Minimal formatting

More formatting


// script to display all the Computers in the Computers table

// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die( "Unable to select database $dbName");

// Select all the fields in all the records of the Computers table
$query = "SELECT *
FROM computers
ORDER BY computerDescription"
;
$result = mysql_query($query);

// Determine the number of computers
$number = mysql_numrows($result);

// print the computer names

print "There are $number types of computers:

";

for (
$i=0; $i<$number; $i++) {
$computerDescription = mysql_result($result,$i,"computerDescription");
print
"$computerDescription
"
;
}

// Close the database connection
mysql_close();
?>


View the source

There are 349 types of computers:

Barbie Computer

Dell Dimension

Dell Inspiron

Dell Optiplex

Gateway 800

Gateway laptop

Hot Wheels Computer

Hp Vectra 500

iMac

Optiplex GX100

Palm IIIX

Sun Ultra 1

TravelMate 5000

View the source of computers.php3


Types of Computers

//include stylesheet for formatting
include("stylesheet.php3");
?>





// script to display all the Computers in the Computers table

// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

// Select all the fields in all the records of the Computers table
$query = "SELECT *
FROM computers
ORDER BY computerDescription"
;
$result = mysql_query($query);

// Determine the number of computers
$number = mysql_numrows($result);

// Print the computer names

print "

There are $number types of computers:


";

for(
$i=0; $i<$number; $i++){
$computerDescription = mysql_result($result,$i,"computerDescription");
/* print even-numbered rows with a gray background,
odd-numbered rows with a white background */
if ($i % 2 == 0) {
print
"";
} else {
print
"";
}
print
"";
}
print
"
$computerDescription
"
;

// Close the database connection
mysql_close();
?>


View the source of computers.php3



View the source of stylesheet.php3


View the source of stylesheet.php3


print "


"
;

?>

One-table query

List of employees

Minimal formatting

More formatting

There are 227 employees:



















































































































































































































Billy Bob
Bugs Bunny
Dubbya Bush
John Doe
Daffy Duck
Elroy Jetson
George Jetson
Bubba JoeBob
Ronald McDonald
Mickey Mouse
Minnie Mouse
Hokey Pokey
Monty Python
Yosemite Sam
Homer Simpson
Joe User
Darth Vader

View the source


// script to display all the Employees in the Employees table

// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die( "Unable to select database $dbName");

// Select all the fields in all the records of the Employees table
$query = "SELECT *
FROM employees
ORDER BY lastName, firstName"
;
$result = mysql_query($query);

// Determine the number of employees
$number = mysql_numrows($result);

// Print the employee names
print "There are $number employees:

";
for (
$i=0; $i<$number; $i++) {
$firstName = mysql_result($result,$i,"firstName");
$lastName = mysql_result($result,$i, "lastName");
print
"$firstName $lastName
"
;
}

// Close the database connection
mysql_close();
?>


View the source

Multi-table query

Inventory

Minimal formatting

More formatting

There are 24 records in the inventory:

Bunny, Bugs, Hot Wheels Computer, 2001-04-15, woo hoo!
Doe, John, Dell Inspiron, 2000-09-15, for home use
Doe, John, Dell Optiplex, 2000-09-19, for home use
Doe, John, iMac, 2000-10-09, john's first iMac
Doe, John, Gateway laptop, 2000-10-17, for business trip
Doe, John, , 2001-06-12, Comment One
Duck, Daffy, Sun Ultra 1, 2000-03-27, departmental web server
Duck, Daffy, Dell Optiplex, 2000-10-05, daffy's dell
Duck, Daffy, Barbie Computer, 2000-09-19, for upcoming business trip
Jetson, Elroy, Dell Dimension, 2000-04-23, Dell Rocks
JoeBob, Bubba, , 2001-04-15, will arrive shortly
McDonald, Ronald, Dell Optiplex, 1999-10-02, on temporary loan
Mouse, Mickey, iMac, 1999-06-03, the purple one
Mouse, Mickey, Dell Inspiron, 2000-07-12, for home use
Mouse, Mickey, Sun Ultra 1, 2000-05-03, mickey's web server
Mouse, Mickey, Dell Dimension, 2000-10-01, for home use
Mouse, Minnie, Dell Dimension, 1999-12-01, sent for repairs on 12/14
Mouse, Minnie, iMac, 2000-05-13, the teal one
Mouse, Minnie, Sun Ultra 1, 2000-10-12, minnie's web server
Pokey, Hokey, Barbie Computer, 2001-03-09, His last computer fell off a wall
Simpson, Homer, iMac, 2000-10-16, on loan for personal use
Simpson, Homer, Barbie Computer, 2000-10-18, for home use
Vader, Darth, Barbie Computer, 2000-10-18, Darth really likes the color
Vader, Darth, Dell Optiplex, 2001-02-01, Please get this to me fast

View the source


// script to display who has which computers

// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

// Select the fields from the appropriate tables

$query =
"SELECT inventory.inventoryID, inventory.dateAcquired, inventory.comments,
employees.firstName, employees.lastName, computers.computerDescription
FROM inventory, employees, computers
WHERE ((inventory.employeeID = employees.employeeID)
and (inventory.computerID = computers.computerID))
ORDER BY employees.lastName, employees.firstName"
;

$result = mysql_query($query);

// Determine the number of records returned
$number = mysql_numrows($result);

// Print the relevant information
print "There are $number records in the inventory:

";

for (
$i=0; $i<$number; $i++) {
$lastName = mysql_result($result, $i, "lastName");
$firstName = mysql_result($result, $i, "firstName");
$computerDescription = mysql_result($result,$i,"computerDescription");
$dateAcquired = mysql_result($result,$i,"dateAcquired");
$comments = mysql_result($result,$i,"comments");
print
"$lastName, $firstName, $computerDescription, $dateAcquired, $comments
"
;
}

// Close the database connection
mysql_close();
?>



View the source

http://itc.virginia.edu/desktop/web/database/inventory/display/home.html

Inserting data into the database

Input form

Add a new computer

Minimal formatting

More Formatting



$hostName
= "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

mysql_connect($hostName,$userName,$password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

$query = "INSERT INTO computers (computerDescription) VALUES ('$computerDescription')";
$result = mysql_query($query);
print
"Data submitted to database!


Computer Description: $computerDescription
"
;

// Close the database connection
mysql_close();
?>

View the source


Computer Added


//include stylesheet for formatting
include("stylesheet.php3");
?>




// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

$query = "INSERT INTO computers (computerDescription) VALUES('$computerDescription')";
$result = mysql_query($query);

print
"Data submitted to database!


$computerDescription
has been added to the Computer List.
"
;

// Close the database connection
mysql_close();
?>


View the source of computers.php3



View the source of stylesheet.php3


Input form Add a new employee Minimal formatting More formatting



$hostName
= "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

mysql_connect($hostName,$userName,$password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

$query = "INSERT INTO employees (firstName, lastName) VALUES('$firstName', '$lastName')";
$result = mysql_query($query);
print
"Data submitted to database!


FirstName: $firstName

LastName: $lastName
"
;

// Close the database connection
mysql_close();
?>

View the source


Employee Added


//include stylesheet for formatting
include("stylesheet.php3");
?>





// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

$query = "INSERT INTO employees (firstName, lastName) VALUES('$firstName', '$lastName')";
$result = mysql_query($query);
PRINT
"Data submitted to database!


First Name: $firstName

Last Name: $lastName

";

// Close the database connection
mysql_close();
?>

View the source


Input form Add a new
inventory record
Minimal formatting More formatting

Add a new record to the inventory:

Date Acquired (yyyy-mm-dd):

Comments:

View the source of this page



// script to collect new records to be added to the Inventory table

// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

// Select all the fields in all the records of the Employees table
$employeeQuery = "SELECT *
FROM employees
ORDER

Add a record to the inventory

Date Acquired (yyyy-mm-dd):

Employee:

Computer:

Comments:

View the source of newInventory.php3

View the source of stylesheet.php3


Add an inventory record


//include stylesheet for formatting
include("stylesheet.php3");
?>




Add a record to the inventory





// script to collect new records to be added to the Inventory table

// connection information
$hostName = "dbm2.itc.virginia.edu";
$userName = "mst3k";
$password = "secret";
$dbName = "mst3k_Inventory";

// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");

mysql_select_db($dbName) or die("Unable to select database $dbName");

// Select all the fields in all the records of the Employees table
$employeeQuery = "SELECT *
FROM employees
ORDER BY lastName, firstName"
;
$employeeResult = mysql_query($employeeQuery);

// Select all the fields in all the records of the Computers table
$computerQuery = "SELECT *
FROM computers
ORDER BY computerDescription"


print "


"
;

?>

PHP

PHP (Hypertext Preprocessor) is a simple scripting language that can be used to create dynamic Web pages. PHP scripts, which are embedded in HTML, are run on the server side, so in order for them to work, the Web server has to be configured appropriately.

The primary purpose of this document is to let you know that PHP is available on the Web servers www.people.virginia.edu, www.virginia.edu, faculty.virginia.edu, indorgs.virginia.edu, and www.itc.virginia.edu. If you would like to use PHP scripts in your personal Web site, you simply need to save a PHP file in your public_html directory with either of the following filename extensions:

  • .php
  • .phtml

Special note: The current version of PHP on www.people.virginia.edu, faculty.virginia.edu, indorgs.virginia.edu, and www.virginia.edu is PHP4. Please note that the .php3 filename extension is still valid, but your scripts will actually run using PHP 4.

See our document on PHP4 and Global Variables to learn what changes you may need to make to your php code:
http://www.itc.virginia.edu/desktop/web/php4.html

If you save the file with a .phps filename extension, you may view the source code (color-coded) from a Web browser.

ITC offers this service to those who have interest in using PHP, but little or no support is offered.

  • If you would like more information on PHP, we recommend you view the following tutorial (which offers links to other tutorials as well):

http://www.php.net/tut.php

  • For an example of using PHP for processing form input, please see:

http://www.itc.virginia.edu/desktop/web/phpform.html

  • For examples of using PHP with a MySQL database, please see:

http://www.itc.virginia.edu/desktop/database/php.html

  • For information about training in PHP and MySQL, please see the Department of Training web site:

http://www.web.virginia.edu/DOTWeb/itccourselist.cfm

Changes to PHP on ITC-maintained web servers

Overview

register_globals is a PHP setting that controls availability of variables that have been submitted by a user to a PHP script (such as data posted from a form, URL-encoded data, or data from cookies). In earlier releases of PHP, register_globals was set to "on", which made for easier, but less secure coding.

ITC is currently in the process of upgrading various web servers to PHP 4.2.1, which has register_globals set to "off". While coding in this environment is slightly more cumbersome, there are substantial security benefits. You may read more about this change in the PHP 4.1.0 Release Announcement.

If you have received notice that your web server is being upgraded and you currently maintain a site that contains PHP, you need to choose from the following two options:

  • To continue using global variables, you will need to follow the instructions for setting up a .htaccess file.
  • If you want to rewrite your code so that it does not rely on global variables (this is strongly encouraged for security reasons), please see the examples section.

If you do not implement one of the above options, your site will likely not work after the upgrade.


Setting up a .htaccess file

If you want to continue using global variables, you will need to create a .htaccess file in your site's main directory (or the main directory of your site that contains PHP scripts). Your .htaccess file should contain the following line:

php_flag register_globals on

We recommend that you create the .htaccess file by logging into your site with SecureCRT, Nifty Telnet, or another telnet application and using a Unix editor (such as pico, jove, vi, or emacs). Windows and Macintosh editors often insert special characters that may corrupt the .htaccess file and make your site unreachable.


Examples of PHP with register_globals set to "off"

The following examples demonstrate how to work with user-submitted data in an environment where register_globals is set to "off". The developers of PHP strongly encourage coding in this way. For more complete documentation, please see the PHP 4.1.0 Release Announcement.

Example of an html form processed by a PHP script

The form below is a plain html form that is processed by a file called register.php3 (in other words, the ACTION of the form is "register.php3"). Each field name in the form is unique and may be referenced as a php variable in register.php3. For example, the html field name for the First Name field is:

firstname

This may be referenced in register.php3 as:

$firstname

register.php3 performs three basic actions:

  • Displays text to the browser
  • E-mails the conference administrator the information supplied from the form
  • E-mails the person who filled out the form a confirmation message

Click here to view the source of register.php3

For more information about availability of php, please see:

http://www.itc.virginia.edu/desktop/web/php.html


Conference Registration:

First Name:

Last Name:

Full e-mail address:

Dinner selection:

Comments:




/* Script to process registration information from home.html */

/* Display text to the browser */
PRINT "
Registration for $firstname $lastname

Registration information for $firstname $lastname


Thank you for registering for the conference!


You will receive confirmation of your registration
via e-mail shortly.";

/* E-mail the conference administrator the information supplied in form */
mail("mpc3c@virginia.edu", "Registration for $firstname $lastname",
"First Name: \t $firstname
Last Name: \t $lastname
E-mail address: \t $email
Dinner selection: \t $dinner
Comments: \t $comments"
, "Reply-To: $email");

/* E-mail the person who filled out the form a confirmation message */
mail ("$email", "Registration Confirmation",
"Dear $firstname $lastname, \n
We have received your conference registration information
and have noted your meal preference of $dinner. Please
let us know if you have any questions or need further
assistance"
, "From:mpc3c@virginia.edu");

?>


View the source


No comments:

Post a Comment