XAMPP Tutorial Notes

Installing Configuring and Developing With XAMPP

NOTE: The article says to start and stop processes by running start and stop.bat files, this is for installed versions. Since we are using XAMPP as a portable application do not use those files to start and stop the servers. Use the XAMPP control panel.

PHP Hello World

PHP is the programming language in LAMP. We will write a small program as an example using Notepad.

Run Notepad and type the following

<html>
<head>
<title> PHP Hello World Example </title>
</head>
<body>
<p><?php echo “Hello World”; ?></p></body>
</html>

Note that php code:

  • begins with a <?php declaration
  • lines of php code end with a semi-colon
  • and the php code
  • ends with a ?>
  • and that the file must have a .php extension.

Only files with .php are executed, files with .htm or .html are ignored and passed straight to the web server for sending to the user’s browser.

Save the file with a filename of helloworld.php to your USB stick in the xampp\htdocs folder.

Goto your web browser and type: http://localhost/helloworld.php

Create a Database and Insert Data

Click the back arrow of your web browser to return to the XAMPP screen

Under Tools, click phpMyadmin

Type in addressbook as the database and click CREATE

Type colleague as the table name with 5 fields and click GO

When creating the id field select the radio button under the primary key icon

When alll 5 fields are defined click SAVE (we might decide to make email or phone allow null values)

NOTE: this page shows you the actual SQL used to create the table. Copy this code to your session log or diary that you are keeping in Notepad.

It also has a create PHP code button that will provide the code to create the table from PHP.Copy the PHP code to your notepad session log/diary.

Click the back button on your web browser

Click on the INSERT menu item at the top of the page.

Select Restart Insertion and select 5 Rows from the dropdown menu

Add the data as shown in the handout by clicking on the text box in the Value column.

DO NOT add a value for the primary key as this has been set to autonumber.

Entering data into mySQL with myPHPadmin

Entering data into mySQL with myPHPadmin

Once the data for the 5 records has been typed in click the GO button on the bottom of the page to insert all 5 records.

Copy the SQL statement into your diary in notepad.

Note that how the first field inserted is just the name of the field with

no specified value – the database will automatically assign id numbers for each record. You might also want to click CREATE PHP code and copy that code to your diary.

Click the BROWSE menu item at the top of the screen to see the data in the database. Note the id numbers.

Write the PHP program

Switch over to Notepad and create a new document and type the code:

<? php// connect and select a database
mysql_connect(“localhost”,”root”,””);
mysql_select_db(“addressbook”);

// Run a Query

$result = mysql_query(“SELECT * FROM colleague”);
?>

Note this uses 3 php functions that work with mysql:

  • mysql_connect connects the web server to the database and requires a host address, username, and password (our mysql database does not have a password yet – it is blank thus the “”)
  • mysql_select_db selects a database and requres a database name (a database server can have many databases)
  • mysql_query sends a SQL statement to the database server and returns a result

We are also using a variable called $result to hold the results of the query. Variables in PHP are indicated by a dollar sign $. A variable is a container that can hold anything we want (sort of like tupperware!).

Now add the following text to the file after the ?> that ends the php code

<html>
<head>
<title> PHP Address Book Example </title>
</head>

<body>
<h1> Address Book </h1>
<table border=”1″ cellpadding=”3″ cellspacing =”0″>
<tr>
<th> ID </th>
<th> First Name </th>
<th> Last Name </th>
<th> Telephone </th>
<th> Email Address </th>
</tr>

This creates a table with column headers corresponding to the fields of our database. What we want to do is to populate the table with the data, thus ensuring an attractive presentation. I changed the values of cellpadding and cellspacing from the handout because it looks nicer.

Now add new PHP code to process the results of the query.

<?php

//loop though all the records

while ($row = mysql_fetch_array($result))

{

echo “<tr>”;

echo “<td>” . $row['id'] . “</td>”;

echo “<td>” . $row['firstName'] . “</td>”;

echo “<td>” . $row['lastName'] . “</td>”;

echo “<td>” . $row['telephone'] . “</td>”;

echo “<td>” . $row['email'] . “</td>”;

echo “</tr>”;

}

This uses a loop that starts with the first record in the result set and continues until there are no more records.

The WHILE condition is TRUE as long as there are records in the result variable. This loop uses another PHP function called

mysql_fetch_array which puts the retrieved data into a list.

The { left squiggly bracket indicates the start of the loop and the right } squiggly bracket indicates the end of the loop.

For each record in the list it the PHP code writes a table row with <TR> and 5 table columns with <TD> and inserts the data from the fields in the database using the $row variable to hold the data.

The period . is used by PHP to add together or concatenate the output, first is the table column <TD> then the actual data followed by the closing tag.

When there are no more records left in the result set then the WHILE condition is now FALSE and the loop ends. Processing then

continues with the next statement.

Now add these lines to finish the program:

//wipe out result data and close connection

mysql_free_result($result);

mysql_close();

?>

</table>

</body>

</html>

We free up the $result variable (because we might re-use the tupperware container to hold something different) and close the database connection.

Note the mysql_close function does not need anything inside the brackets – this closes all database connections.

Now save the notepad file as address.php in the xampp\htdocs folder of your USB stick.

Goto your web browser and type in http://localhost/address.php

You will probably get an error like:

Parse error: syntax error, unexpected

which means you have a typo somewhere, usually a missing bracket, or quote. Check your code, fix it, re-save and try again. When successful you will see:

Display of the PHP program

Display of the PHP program

About these ads

One Response to XAMPP Tutorial Notes

  1. alleningrad says:

    FYI:

    MAMP runs well on my Mac OSX, allowing Apache and mySQL applications to run. The coding is the same for creating the database exercise.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: