Creating Dynamic Web Sites with PHP and MySQL
General information PHP
is a programming language that lets you develop interactive Web sites.
If you use it in conjunction with a MySQL
database, you can build applications that collect information via
the Web, store it in a database, and provide a wide range of reporting
options.
MySQL is only available on request. If you
want to use MySQL, please send a request to hosting@vt.edu.
We do not offer individual consulting on the use
of PHP and MySQL. There are many good PHP-related resources freely
available
on the Web. Please refer to the PHP
Web site or use a search engine like Google
for more information. Please contact hosting@vt.edu
for technical asistance only after you are confident that you encountered
a problem with the server setup.
Using the MySQL Database
Once enabled, there is exactly one database for your Web
site. Everyone who uses your Web site will share this database. However,
there is no limitation on the number of tables that you can create
within your database.
To manage your database, go to the Web
Administration Tool, and log on by providing your Web site
address, PID, and password. Select the Database tab on top of
the page. On
top of the screen, you will notice information about the database
name, user-ID, and password. You will need all of them to connect
to your database via PHP.
To create a table in your database,
just click the SQL tab and specify
the data structure. Here is an example:
CREATE TABLE test (
firstname text,
lastname text
);
INSERT INTO test (firstname,lastname) VALUES ('Joe','Smith');
INSERT INTO test (firstname,lastname) VALUES ('Mary','Jones');
INSERT INTO test (firstname,lastname) VALUES ('Thomas','Miller');
|
Here is an example of PHP code that connects to the database,
retrieves information and outputs it to a Web browser.
Substitute the database name that is shown on the database
tab in the Web administration tool for "database1".
Substitute the User-ID that is shown on the database tab in
the Web administration tool for "site1".
Substitute the password that is shown on the database tab
in the Web administration tool for "Kkw87%3E".
<?php
require_once('DB.php');
$mydbhost = "database.hosting.vt.edu";
$mydatabase = "database1";
$mydbuserid = "site1";
$mydbpassword = "Kkw87%3E";
$database = DB::connect( "mysql://".$mydbuserid.":".$mydbpassword."@".$mydbhost."/".$mydatabase );
$result = $database->query( "SELECT * FROM test ORDER BY lastname" );
$rows = $result->numRows();
for ($i=0; $i < $rows; $i++) {
$data = $result->fetchRow(DB_FETCHMODE_ASSOC, $i);
$firstname = $data['firstname'];
$lastname = $data['lastname'];
echo $firstname," ",$lastname,"<br>";
}
$database->disconnect();
?>
|
If you execute the script above, your Web browser will show:
Mary Jones Thomas Miller Joe Smith |
You can find more detailed information in the Creating
Database-driven Websites with PHP&MySQL tutorial.
|