You are on page 1of 13

CoreiTech Solutions – PHP & MySQL Crash Course

CoreiTech Solutions
PHP-MySQL Crash Course

Goal
Learn the basics of PHP and MySQL Development in two days. This is a crash course so you
must be fast in absorbing the topics discussed in all modules.

Motivation
Learning the fundamentals of data-driven websites using PHP and MySQL will give you the
necessary skills for entry-level web development projects. The topics covered in this manual are
important in order to prepare you for advanced topics as well as to equip yourselves with the skills
needed for our first major project. It is important to understand each topic and not to skip topics even if
you already know about it. Remember that without basic skills, you will not improve, so it is a must for
everyone to improve their basic web development skills before advancing to higher topics.

What you should already know

1. HTML / XHTML
2. Foundation on a C-like Language (C, C++, Java)
3. Basic Arithmetic Concepts (Operators, Variables)

Tools

July 10, 2010


1. WampServer (Package containing PHP, Apache and MySQL)
2. Mozilla Firefox (WampServer favors Firefox over other browsers and Firefox conforms to web
standards that’s why this would be the browser we will use most of the time.)
3. Eclipse PHP (Our programming / coding tool)
4. Navicat MySQL (GUI for Creating MySQL Databases)

PHP & MySQL Crash Course – Day 1


CoreiTech Solutions – PHP & MySQL Crash Course

PHP & MySQL Crash Course


Part 1 – Setting-up Our Development Environment

Before we could develop websites in PHP, we must first install the necessary tools / programs.

WampServer – is a package containing PHP, Apache and MySQL. WAMP stands for Windows Apache
MySQL. During the early days when packages are not yet available, you need to install PHP, Apache and
MySQL separately and do the necessary configurations in order for these three programs to work
together. Thanks to WampServer we no longer need to install these programs separately. When you
install WampServer, it installs these three applications and do some default configurations. There are
times when we need to do additional configurations to these programs but that depends on the
requirements of the application we need to develop. For this tutorial, the default configuration is
enough.

To install WampServer, make sure you already have Firefox. I prefer using Firefox 3.5 or higher
preferably 3.6. If you do not have Firefox, install it before installing WampServer. Installing WampServer
is easy, just run the installer and follow the instructions. During WampServer installation, WampServer
will detect Firefox and asks you if you want to use Firefox as your default browser for your Wamp
installation. Just click yes and proceed until the installation is finished.

After WampServer is installed, you will see this icon on the desktop:

July 10, 2010


PHP & MySQL Crash Course – Day 1
CoreiTech Solutions – PHP & MySQL Crash Course

Now we need to check if our installation was successful. Double click the icon on the desktop. You
should see an icon on the taskbar like the one below.

After you launched the WampServer icon on the desktop, you should see this icon. While WampServer is loading,
you may see this icon in color red. Just wait until Wamp completed its initialization stage and this icon will turn to
white.

WampServer status indicators:

All Services are running One of the two services MySQL and Apache services
(MySQL and Apache) (MySQL or Apache) may not are NOT running
be running

July 10, 2010


PHP & MySQL Crash Course – Day 1
CoreiTech Solutions – PHP & MySQL Crash Course

PHP & MySQL Crash Course


Part 2 – PHP Basics

Before we can create our first PHP script, we must know first where to save our PHP files. In Wamp, we
save our PHP scripts under C:\wamp\www\ We call this folder location as the “WEB ROOT”. In order
to simplify instructions, I will use the word WEB ROOT across this document in place of the
C:\wamp\www.

Wamp provides a very easy to use UI (User Interface) in order to easily go to this folder location. Left
click the Wamp icon on the system tray, and click www directory.

July 10, 2010


PHP & MySQL Crash Course – Day 1
CoreiTech Solutions – PHP & MySQL Crash Course

Part 2 – PHP Basics: Our First PHP Script

We will start by using notepad. So open your notepad and type the following:

<?php
echo „My First PHP Script!‟;
?>

Save this on your WEB ROOT and name this file php1.php

To test this script make sure you open first your wampserver and the status indicators must be ok.
Open Firefox and navigate to http://localhost/php1.php

July 10, 2010


PHP & MySQL Crash Course – Day 1
CoreiTech Solutions – PHP & MySQL Crash Course

IMPORTANT NOTES:

1. You must save all your PHP scripts inside the WEB ROOT. You can create folders inside the web
root. For example if you are developing multiple projects, you can create folders for each
project such as C:\wamp\www\project1 and C:\wamp\www\project2.php. If your
PHP files are inside subfolders, you would need to access them in the browser like this:
http://localhost/project1/index.php
2. You CANNOT run a PHP web page or script just by double clicking it. Why? Because PHP
is a Server-Side Scripting Language (a.k.a Programming Language for the Web). Web
browsers only know client-side languages such as HTML, JavaScript. When we say server-side
language, we are referring to a language which must be processed FIRST ON THE SERVER,
before on the client (browser). The server that we are referring to for PHP is the Apache.
3. Apache is a server program that INTERPRETS PHP codes and converts them to an output that
is understood by the browser (usually HTML). Without the Apache, we cannot run our PHP
script (unless you know how to setup IIS). It’s just like having a Chinese along with you. Let’s
say the Chinese is the PHP, and you are the Web Browser (Firefox). If only the two of you will
talk to each other, you as the browser will not be able to understand the Chinese (PHP), so in
order for you to understand what the Chinese is saying, you need an INTERPRETER. Just like
in beauty contests. Apache is our PHP interpreter so that Firefox would be able to understand
what PHP is saying. An interpreter is the equivalent of a COMPILER in programming.

July 10, 2010


4. Why do we need to save our PHP scripts in WEB ROOT? Simple, Apache looks for PHP files in
this location. So if you will not save your files on the WEB ROOT, you will not be able to run
your PHP-based websites.

PHP & MySQL Crash Course – Day 1


CoreiTech Solutions – PHP & MySQL Crash Course

In order for you to understand clearly what a server-side language is, test again your page on Firefox by
navigating to http://localhost/php1.php . Then on Firefox click View -> Page Source. You will see that
the only thing left on the source of the page is this:

All the server-side codes like the <?php, ?>, echo and ; are all stripped away. Apache is the one
responsible for this. Apache interpreted the PHP code and return only the output that could be
understood by the web browser, in this case “My First PHP Script!”.

July 10, 2010


PHP & MySQL Crash Course – Day 1
CoreiTech Solutions – PHP & MySQL Crash Course

Part 2 – PHP Basics: The echo function

In our first example we used the function echo to print output to the browser. To use echo, you must
code it in this form:

echo „<any string you want to output>‟;

Try this code:

<?php
echo „<div><h1>Hello World!</h1></div>‟;
echo „<p>CoreiTech!</p>‟;
?>
Experiment on the echo statement. You can use echo to print HTML tags. You can even use PHP
to generate CSS and many more.

July 10, 2010


PHP & MySQL Crash Course – Day 1
CoreiTech Solutions – PHP & MySQL Crash Course

Part 2 – PHP Basics: Variables

Variables are allocation in the memory which allows you store values you will need later on. The value
of the variables can be changed anytime during compile-time or run-time.

PHP is a loosely-typed language. This means that you don’t need to tell what data type your variable
is. The data type is evaluated at runtime.

PHP is C-like in syntax and form. But unlike C-like languages (C, C++, JAVA), PHP differs in the way we
define variables. For example:

C++ / JAVA Variable declaration: int studentAge = 30;

PHP Variable declaration: $studentAge = 30;

To define a variable in PHP, we use the dollar sign ($). And as you can see, you no longer need to say
what the data type of the variable is.

$studentName = “Llamado, Ayra”; //this is a String

$studentAge = 21; //this is an int

$studentAge = “21”; //this is a String

July 10, 2010


$moneyOnPocket = 30.50 //this is a Float

PHP assumes the data type of the variable based on its value.

PHP & MySQL Crash Course – Day 1


CoreiTech Solutions – PHP & MySQL Crash Course

Part 2 – PHP Basics: Displaying variable contents

Using Notepad, create a file named php2.php and save your file on the WEB ROOT. Type the following
codes;

<?php
echo „<h1 align=”center”>PHP Variables</h1>‟;

$memberName = “Lasay, Markhem”;


$memberAge = 21;
$memberAddress = “Lag-on, Daet, Camarines Norte”;

$justAVariable = “33”;
$anotherVariable = “46”;

echo $memberName; //Note: No single quotes.


echo $memberAge;
echo $memberAddress;
echo $justAVariable + $anotherVariable;
?>
Test this page by navigating on Firefox to: http://localhost/php2.php

July 10, 2010


NOTE: Have you noticed the output of this script? Before you proceed to the next slide, try to fix the
ouput of this script so that it could easily be understood.

PHP & MySQL Crash Course – Day 1


CoreiTech Solutions – PHP & MySQL Crash Course

Part 2 – PHP Basics: A better way of displaying variable contents

Using Notepad, edit the named php2.php and save your file on the WEB ROOT. Type the following
codes;

<?php
echo „<h1 align=”center”>PHP Variables</h1>‟;

$memberName = “Lasay, Markhem”;


$memberAge = 21;
$memberAddress = “Lag-on, Daet, Camarines Norte”;

$justAVariable = “33”;
$anotherVariable = “46”;

echo “<p><strong>Name:</strong>$memberName</p>”;
echo “<p><strong>Age:</strong>$memberAge</p>”;
echo “<p><strong>Address:</strong>$memberAddress</p>”;
echo “<p>” . $justAVariable + $anotherVariable .”</p>”;
?>
Test this page by navigating on Firefox to: http://localhost/php2.php

July 10, 2010


NOTE: The dot (.) is used to CONCATENATE the strings and variables. Concatenate means joining.
For example “Hello” . “ World” will result in Hello World.

PHP & MySQL Crash Course – Day 1


CoreiTech Solutions – PHP & MySQL Crash Course

Part 2 – PHP Basics: THE BEST WAY to display PHP output : Embedding PHP

It is really a pain in the neck to always echo HTML tags such as <div>, <p> etc. just to format your
output. Good thing is that PHP can be embedded in HTML!

Create a file named php3.php and save it in WEB ROOT. Using Notepad, write the following codes:

<html>
<head>
<title>Embedding PHP in HTML</title>
</head>
<body>
<div><?php echo ‘CoreiTech Solutions’; ?></div>
</body>
</body>
</html>
NOTE: As you can see, we can embed PHP inside HTML tags. Since you already had experience with
XHTML, this would be easy for you. The process would be start designing your page in XHTML and / or
with CSS. Then rename your file and change the .html extension to .php Then embed your PHP codes
just like the code above. A PHP code that is embedded on an HTML code is called a SCRIPTLET. Other

July 10, 2010


developers call it as PHP ISLANDS.

PHP & MySQL Crash Course – Day 1


CoreiTech Solutions – PHP & MySQL Crash Course

EXERCISE
Revise your XHTML project named I am a CoreiTech Member. This
time, rename your html files to PHP and embed PHP scriplets in your
files.
It’s up to you what messages you would embed in your HTML page
as long you practice all that’s been discussed today.
o Variables
o echo
o Concatenation
You must create individual folders inside the WEB ROOT. For
example for Ayra, create a folder named allamado inside WEB
ROOT (C:\wamp\www\allamado) and save all your files inside that
folder

END OF MORNING SESSION! 

July 10, 2010


PHP & MySQL Crash Course – Day 1

You might also like