This document assumes that you've done the
MySQL installation and setup.
The test programs below assume that MySQL test database is
accessible for the guest user with empty password.
The software packages you'll need are these installation files:
NetBeans is an optional Php project editor.
As of version 6.5, this has become
an acceptable choice for working with Php-based projects.
NetBeans does, however,
require a the Java
component including the Java Development Kit (JDK) installation.
You need to do the steps from
Netbeans/JDK Installation, omitting
the Hello World Program section and beyond.
After this, the software package you'll need to install is:
When you install it, there will be a point where it asks you to enter
server information:
Network Domain put "localdomain" here
Server Name put "localhost" here
Administrator's Email Address put anything, like "a@b.c" here
* For all users on port 80 the default, probably want this
For current user on port 8080 if you want IIS as your main web server
To control/configure Apache, follow this chain:
Programs
Apache HTTP ...
Configure ...
Control ...
Edit the Apache httpd.conf ...
The Control choices
include Start, Stop and Restart.
If everything goes OK the Apache service will start automatically
after its installation and restart when you reboot the computer.
The Configure menu choice contains the important Edit
choice for modifying the key configuration file
httpd.conf in the conf subdirectory
of the Apache installation.
Whenever you make an editing change to this file, you need
to restart the server to pick up the changes.
Basic Test of the Apache installation
Open up an internet browser and enter the URL for your local site:
You should see the Apache default root page. If for some reason this
doesn't appear, the Apache Server may need to be started.
For Windows, as usual, consider rebooting the computer if it doesn't
work.
Create a default URL
Apache on Linux and modern Windows systems
can be configured to support the association of the URL
http://localhost/~YOUR_LOGIN
to a specific directory on your system, but for
convenience of exposition, it is easier to use a URL name
that does not refer to a specific user name.
Assuming your are the only user
on your system, this is not a bad idea anyway.
So
we will set up a
desired directory to correspond to the neutral URL:
http://localhost/default
Any directory can serve as the Web default directory.
Here are two possibilities:
Some directory in your C:\ drive, like:
C:\public_html
Your NetBeans project directory
C:\Documents and Settings\YOUR_LOGIN\My Documents\NetBeansProjects
We want to map the URL http://localhost/default to
this directory.
Edit the httpd.conf configuration file, most likely by
choosing the Apache program menu selection:
Edit the Apache httpd.conf Configuration file
Go to the end of the file and add the following lines.
The choices here are based on the two possible default
directories:
Alias /default "C:\public_html"
<Directory "C:\public_html">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
In this case be sure to modify YOUR_LOGIN appropriately:
Alias /default "C:\Documents and Settings\YOUR_LOGIN\My Documents\NetBeansProjects"
<Directory "C:\Documents and Settings\YOUR_LOGIN\My Documents\NetBeansProjects">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Afterwards this test you can delete hello.html since
it has no further usage.
Php
Php can actually be used in one of three variants:
as an Apache module
as an Web-based CGI programming language; especially in
conjuction with FastCGI
as a command-line interpreter
We'll be primarily interested in the first of these choices.
The Php distribution for Windows includes the
zip package that we'll use as well as an installer file.
I prefer the zip package because it is relatively easy to do the
installation by hand and then you know exactly what's happening.
You have to decide upon an installation directory.
I will assume that this
is the installation directory
C:\php
You may need to create this and/or save an previous version, if so desired.
If you choose a different installation directory, you'll
need to appropriately modify later details in this handout.
Extract php-5.2.9-2-Win32.zip into C:\php.
Then (assuming Windows XP or later)
copy
C:\php\php5ts.dll into C:\windows\system32
copy C:\php\libmysql.dll into C:\windows\system32
copy C:\php\php.ini-dist into C:\php\php.ini
You may need to copy other .dll files into
C:\windows\system32
based on other desired Php functionality.
The file php.ini is Php's configuration
file to which you'll want to make a number of editing changes.
Php offers two choices:
php.ini-dist and php.ini-recommended.
The former represents the default initialization settings that are
built into Php which are better for learning and developing Php,
since it gives feedback such as warnings and errors on
the web page itself.
The latter version
establishes more security features; for example, it
does not display error information,
preferring to log the errors
only this is better for a production environment where you don't
want a mistake to reveal too much to the public.
Modifications to php.ini
Edit the Php configuration file C:\php\php.ini and
make the following changes.
Magic quotes.
This is a nuisance that is supposed to be
helpful for inserting form entry data into a database.
Look for the magic_quotes_gpc setting
(gpc means for GET/POST/Cookie values).
magic_quotes_gpc = On
Change it to
magic_quotes_gpc = Off
Session support.
This step is needed to support sessions.
First, create a subfolder by the
name tmp within the php this folder.
Then look for the (commented out) line in the session section:
;session.save_path = /tmp
This default setting is suitable for UNIX systems, but not for Windows.
Uncomment out this line and rewrite it using the directory you just
created, which, according to my standards, is:
session.save_path = C:/php/tmp
Modifications to Apache config file
Edit the Apache configuration file httpd.conf by
going to the end of it and adding these lines (if necessary,
change occurrences of C:/php to reflect your installation):
<?php
session_start(); // the FIRST THING YOU DO!
// to see sessions "not work", comment out the "session_start();" line
if (!isset($_SESSION['test'])) {
echo "First activation: setting session variable";
$_SESSION['test'] = 1;
} else
echo "SESSIONS ARE WORKING! activation: ", (++$_SESSION['test']);
?>
You should see them listed in
http://localhost/default/
or you can activate them through these URLs:
Regarding the session-test.php script,
if you key it in or copy it into a blank file,
make sure there are no whitespace characters
before "<?php".
You will know if sessions are working properly by
pressing the browser's refresh button in which case
you you should see the "SESSIONS ARE WORKING"
validation message.
NetBeans/Php Hello World
If you want to use NetBeans, you'll have to have done the
standard Java installation as described in the
NetBeans/JDK document.
Start up NetBeans and
follow these steps to create a "Hello World" project in your default directory.
Select File New Project
In the New Project window,
select the PHP category,
and choose PHP Application, then Next.
It appears to be a bit tricky to get the order of operations just right, but
you want:
the Project Name to HelloPhp.
the Sources Folder to be the HelloPhp folder within
your default website folder.
In the Run Configuration window, choose
Run As: Local Web Site
(the default)
Project URL: http://localhost/default/HelloPhp/
Click Finish. This will create the default file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
</head>
<body>
<?php
// put your code here
?>
</body>
</html>
In the Projects Window, right-click on the newly-created HelloPhp
project and select Set as Main Project.
Replace the
"// put your code here",
line by
echo "Hello from NetBeans";
Select Run Run Main Project.
This should run the script in new browser window (or frame).
Php/MySQL PDO API
Php (version 5 and above)
supports multiple ways to access a MySQL DBMS, including
the PDO API as well as other older alternative APIs.
Edit the Php configuration file C:\php\php.ini.
Search for the extension_dir variable.
Change the default setting (probably "./") to this:
extension_dir = "C:/php/ext/"
In the "Windows Extensions" section look for these commented out
lines and uncomment them by removing the initial semicolon, getting:
The PDO is one of the more modern APIs for Php. It supports
the database abstraction like Java JDBC and its programmatic
usage falls very much along the same lines with exception-generating
actions contained within a try-catch block.
Download (via the following link) or create the file
mysql-pdo-test.phpbelow in default directory. Then test it via the following link
in which you should see "SUCCESS!" printed.
This is the original API used by Php. The disadvantage
of this and the subsequent MySQLi API is that they are linked
directly to the MySQL connection, and not abstractions.
However, they do provide a more direct level of access in
that they do not need specify a database in order to connect.
In fact, they are at the same level as the MySQL command-line
interpreter and
thus allow more MySQL-specific command usage.
Download or create the file
mysqltest1.php below in default directory. Then test it via the following link
in which you should see "SUCCESS!" printed.
<h3>Standard mysql interface</h3>
<?php // mysqltest1.php
echo "-->connect:\n";
$dbh = mysql_connect( "localhost", "guest", "" ) or die(mysql_error());
echo "<br />-->select database:\n";
mysql_query( "USE test", $dbh ) or die(mysql_error());
echo "<br />-->create table:\n";
mysql_query(
"CREATE TABLE IF NOT EXISTS test_tab (thing VARCHAR(10))", $dbh )
or die(mysql_error());
echo "<br />-->insert into table:\n";
mysql_query( "INSERT INTO test_tab VALUES ('book'),('pencil')", $dbh )
or die(mysql_error());
echo "<br />-->select from table:\n";
$res = mysql_query( "SELECT thing FROM test_tab", $dbh )
or die(mysql_error());
while( list($thing) = mysql_fetch_row($res) ) echo "$thing ";
echo "<br />-->drop table:\n";
mysql_query( "DROP TABLE test_tab", $dbh )
or die(mysql_error());
echo "<br />SUCCESS!";
?>
The MySQLi API
The second test uses the new mysqli interface which
was started in Php version ≥ 4.1.2.
This interface exploits functionality only available in
MySQL versions ≥ 4.1.
The mysqli interface provides extended functionality
as well as both a procedural and object-oriented interface
to SQL operations.
Edit the Php configuration file C:\php\php.ini
Look for the "Windows Extensions" section and look for the
line used previously:
extension=php_mysql.dll
Underneath that line add this new line (note the difference):
extension=php_mysqli.dll
and then restart Apache.
Download create the file
mysqltest2.php below in default directory. Then test it via this link:
<h3>The mysqli object-oriented interface</h3>
<?php // mysqltest2.php
echo "-->connect:\n";
$dbh = new mysqli( "localhost", "guest", "" );
if (mysqli_connect_error())
die("failed: " . mysqli_connect_error());
echo "<br />-->select database:\n";
$dbh->query( "USE test" ) or die($dbh->error);
echo "<br />-->create table:\n";
$dbh->query(
"CREATE TABLE IF NOT EXISTS test_tab (thing VARCHAR(10))" )
or die($dbh->error);
echo "<br />-->insert into table:\n";
$dbh->query( "INSERT INTO test_tab VALUES ('book'),('pencil')" )
or die($dbh->error);
echo "<br />-->select from table:\n";
$res = $dbh->query( "SELECT thing FROM test_tab" )
or die($dbh->error);
while( list($thing) = $res->fetch_row() ) echo "$thing ";
echo "<br />-->drop table:\n";
$dbh->query( "DROP TABLE test_tab" )
or die($dbh->error);
echo "<br />SUCCESS!";
?>
PEAR MDB2 API
The third test uses the PEAR MDB2 class.
PEAR is an acronym for the PHP Extension and
Application Repository.
PEAR development has a life of its own based at the home page:
The MDB2 class provides an abstract interface to a number of
Database Systems, including, of course, MySQL.
The important difference between this API verses the
MySQL and MySQLi APIs is that the connection needs to know the data source,
which is constituted of the DBMS plus a specific database.
In this sense it is less capable than the others, but it gains in the
ability to function with multiple database systems
with little or no change to the code.
PEAR and MDB2 package installation
You need internet connection to do this.
Double-click to run the C:\php\go-pear.bat batch file.
Make sure you are using this most recent version of Php, because
earlier distributions had a bug in this batch file.
More than likely you'll want to take the defaults on all the questions
asked by simply pressing enter.
Part of the execution of the script involves downloading
and installing the standard packages.
At the end of the script a message is given about double-clicking
the file
C:\php\PEAR_ENV.reg
to install a registry variable needed for pear, so
go ahead and do that.
After this installation, restart Apache.
The pear executable offers a command-line interface for
determining available packages and installing them. We will use this
to install the MDB2 package. Open a command shell and
run these commands: