You are on page 1of 34

About the Author
Ivan Bayross the human being

Open Source Varsity

The author Ivan Bayross is a 58 year old techno geek, mentor and friend. He is an M. Tech from Manchester University UK and has spent more than 30 years in the software industry rising through the ranks in the IT industry. Ivan Bayross started as a programmer, moved up to CIO, and is now the Chairman and Technology lead, in a software development house at Mumbai, India. He also owns several businesses in Mumbai India. He specializes in multi-tier, client / server, application development. Ivan Bayross is really a most approachable person and happily shares his skills with students and professionals alike.

Ivan Bayross the teacher
In the last ten years Ivan Bayross has put aside one day in the week when he guest lectures at a few MBA and Engineering colleges. If you would like him to guest lecture at your college, do get in touch.

Ivan Bayross the consultant
Additionally, being passionate about project management, he has been assisting a number of medium sized I.T. houses in stream lining their project management processes. Assisting them in any way he can to deliver their throughput to their clients in the shortest possible time and with the least bugs possible. Ivan Bayross is also very interested in Human Resource Management. HR is the most valuable resource in any company, more so today, because of the very competitive business environment in existence. He has assisted a number of medium sized companies in improving their Human Resource Management processes, helping them retain their talent, appropriately hire new talent, and cross train and improve their current employees to deliver a ton more than they currently do.

Ivan Bayross the author
Ivan Bayross is also an author, having authored and published over 68 books. He has the ability of taking a fairly complex programming or design concept and explaining this in simple to understand terms which has won him tons of fans among young information technology professionals.

© Open Source Varsity. All Rights Reserved.

Open Source Varsity

Table of Contents
PHP INTRODUCTION .......................................................................................................................................................... 4 WHAT IS PHP? ........................................................................................................................................................................ 4 HOW PHP WORKS? ................................................................................................................................................................ 4 WHAT ARE PHP BENEFITS? .................................................................................................................................................... 5 WHAT IS A PHP FILE? ............................................................................................................................................................. 5 WHY PHP? .............................................................................................................................................................................. 5 PHP DOWNLOAD .................................................................................................................................................................. 6 DOWNLOAD PHP ................................................................................................................................................................... 6 A COMMON TECHNIQUE TO FIND PHP {ANYTHING} USING GOOGLE ................................................................................. 6 PHP INSTALLATION .......................................................................................................................................................... 10 UNINSTALLATION PHP .................................................................................................................................................... 12 BINDING PHP TO APACHE .............................................................................................................................................. 15 CONFIGURING PHP TO WORK WITH APACHE: .................................................................................................................... 15 SYSTEM SETTINGS IN WINDOWS XP - PATH ......................................................................................................................... 16 TESTING THE APACHE AND PHP RELATIONSHIP ................................................................................................................. 18 BINDING PHP TO MYSQL ................................................................................................................................................ 20 INTRODUCTION: .................................................................................................................................................................... 20 THE BINDING PROCESS: ........................................................................................................................................................ 20 PHP / MYSQL - AT THE COMMAND PROMPT ........................................................................................................... 23 SETTING THE PATH TO THE PHP RUNTIME IN WINDOWS XP .............................................................................................. 23 CREATING PHP FILES: ........................................................................................................................................................... 25 Creating PHP file For MySQL Connection: ................................................................................................................. 25 Creating PHP File for MySQL Database: ..................................................................................................................... 26 The Structure Of The MySQL StudentMaster Table: ................................................................................................. 27 Creating PHP File for MySQL Database Table: .......................................................................................................... 28 Executing PHP file at Command Prompt: ................................................................................................................... 29 Verifying the Database and Table Is Created: ............................................................................................................. 31 MYSQL AND MYSQLI ........................................................................................................................................................ 34

3

© Open Source Varsity. All Rights Reserved.

on the fly and automatically. HTML that has been correctly loaded with appropriate. general-purpose. The Web server then calls the PHP script named in the URL or perhaps index. If a number is assigned to a variable.php> requested for by a Browser and delivered by the web server. PHP takes care of converting the variable from an integer-type to a string-type. Memory variables are not declared to be of a specific type before they can be assigned a value to hold. . PHP really has a very simple way of working with different types of memory variables. then it just works. the Browser sends out a request to the Web server. The PHP environment at the Web server executes the script. All Rights Reserved. What PHP does is that it pre-processes the contents of any <filename. They are simply created and assigned a value when required for use. the output of the PHP script is pure HTML which is then sent back to the requesting Browser as shown in Diagram 1. Hypertext (HTML) Pre Processor. PHP is not strongly typecast. Pre-processing of <filename. When the variable is later outputted to the browser. which has great power for manipulating. PHP is especially suited for Web development. created to run successfully with a host of Web servers. When you type in the URL of a PHP website in a Browser’s address bar and hit the GO key. Diagram 1 4 © Open Source Varsity. well formatted and styled content. it just works.PHP Introduction What is PHP? Open Source Varsity PHP is a widely used Open Source. HTML can be embedded into PHP.php> ensures that only pure HTML is delivered to the requesting Browser. Web server-side scripting language. PHP is often referred to as a Hypertext Preprocessor and is always used in conjunction with a web server such as Apache. PHP memory variables need not be pre-declared. How PHP Works? PHP sits between a Browser and a Web server.php which can be visualized as a gateway script. formatting and styling data and delivering this to a requesting Browser. PHP is actually a simple language. PHP is actually an interpreter based. The fact that PHP runs at the web server has several benefits and a few drawbacks.

One primary example is Yahoo! Bookmarks. Open Source Varsity That means you don't have to pay thousands of dollars in licensing fee to acquire PHP. PHP is a purpose built language for the internet. IIS. PHP has an ever growing community. forums and community websites dedicated to PHP.What are PHP benefits? PHP is a free. PHP has proven itself to be extremely secure and more than sufficient to run high risk sites such as banking or ecommerce applications. HTML tags and scripts PHP files are returned to the browser as plain HTML PHP files have a file extension of ". free server side application that can be installed on any computer in the world. open source. There are thousands of websites on the internet which are written using PHP.) PHP is compatible with almost all servers used today (Apache. it is easy to install.) PHP is FREE to download from the official PHP resource: www. even on extremely high traffic websites. The most striking feature of it is that it is easy to learn. or ".php3".php. This compatibility and extremely economical running costs makes PHP the number one choice for businesses around the world. All Rights Reserved. assistance for PHP issues is always available most often for Free or if charged for the charges are always pretty modest. . It is specifically designed to run on web servers to deliver dynamic web pages as quickly as possible. PHP has the huge advantage of being supported by virtually every server in the world. Hence. Web server side scripting language. Unlike other languages that require expensive software to be installed on the server. PHP just requires Apache. Best of all. Linux. ". Unix. Security is a huge factor in today’s' IT world. a lightweight. What is a PHP File? • • • PHP files can contain text. etc.net PHP is easy to learn and runs efficiently on the server side 5 © Open Source Varsity.phtml" Why PHP? • • • • PHP runs on different platforms (Windows. PHP is used by millions of people and developers around the world. etc.php". There are thousands of websites.

Diagram 2 6 © Open Source Varsity.net/downloads.google. All Rights Reserved. . A Common Technique to Find PHP {anything} Using Google Open a browser window and enter http://www. Diagram 1 In the Search results window Click on the php.PHP Download Download PHP Open Source Varsity Download the latest stable build of PHP from URL http://www.php . In the Google search window that opens search for php download as shown in Diagram 1.com in the address bar and hit GO.net/downloads.php link as shown in Diagram 2.php.

When the php. Open Source Varsity Diagram 3 Click on the zip file of Thread safe version of PHP as shown in Diagram 4. All Rights Reserved.net site opens in the Browser window click on the windows binary link as shown in Diagram 3. 7 © Open Source Varsity. .

.Open Source Varsity Diagram 4 8 © Open Source Varsity. All Rights Reserved.

All Rights Reserved. .Save the zip file in the local drive of you system by clicking on the OK button of the window as shown in Diagram 5. 9 © Open Source Varsity. Open Source Varsity Diagram 5 The download process of PHP is successfully finished.

Diagram 7 Extract all the files from the . .zip file to C:\php as shown in Diagram 8.PHP Installation Open Source Varsity Navigate to the folder where the PHP executable . All Rights Reserved. Diagram 6 Create a subdirectory in drive 'C' named C:\php as shown in Diagram 7. 10 © Open Source Varsity.zip was downloaded to as shown in Diagram 6.

All Rights Reserved. 11 © Open Source Varsity.Open Source Varsity Diagram 8 The installation of PHP is complete. .

navigate to C:\WINDOWS and search for a file named php. All Rights Reserved. Next. Open Source Varsity Diagram 9 PHP is now totally removed from the system. Shift /Delete this file from C:\WINDOWS as shown in Diagram 10.Uninstallation PHP Uninstalling PHP from a system requires a few easy steps. Shift / Delete the folder from the drive as shown in Diagram 9. .ini and select the file. 12 © Open Source Varsity. Select the folder. Navigate to the drive where the php folder is located.

All Rights Reserved. 13 © Open Source Varsity.Open Source Varsity Diagram 10 Next. .dll and select the file. navigate to C:\WINDOWS\system32 and search for a dll named libmysql. Shift / Delete this file from C:\WINDOWS\system32 as shown in Diagram 11.

. 14 © Open Source Varsity. All Rights Reserved. It’s as simple as this.Open Source Varsity Diagram 11 PHP has been completely removed from the system.

AddType application/x-httpd-php .2\conf subdirectory.opensourcevarsity.com/apachebasics/l3apachewebserverinstallation Configuring PHP to work with Apache: To bind Apache and PHP together a few changes have to be made in Apache's httpd. amd add another LoadModule entry.conf file is normally found in: C:\Program Files\Apache Software Foundation\Apache2. .Binding PHP to Apache Note Open Source Varsity To know How to Install Apache Web Server refer to http://www. These are the entries that have to be made in Apache's httpd.conf file locate the LoadModule section.gz .tgs add another entry as shown in Diagram 12. Locate the IfModule mime_module section in the httpd.php Take a look at the diagram below it could help you out Diagram 12 Within Apache's httpd. Apache's httpd. 15 © Open Source Varsity.conf file.conf file Below the AddType application/x-gzip . All Rights Reserved.conf.

My Computer > Property > Advance > Environment Variable >System Variable – Path. Take a look at the diagram below it could help you out Open Source Varsity Diagram 13 Apache and PHP have been configured to work together System settings in Windows XP .LoadModule php5_module "C:/php/php5apache2_2.dll" as shown in Diagram 13.Path Right click My Computer icon on the desktop and go to Properties. System Properties window open as shown in Diagram 14. and add C:\php as shown in Diagram 16. All Rights Reserved. 16 © Open Source Varsity. .

All Rights Reserved.Open Source Varsity Diagram 14 Diagram 15 17 © Open Source Varsity. .

Open Source Varsity Diagram 16 System settings in Windows XP completed Testing the Apache and PHP relationship Using any ASCII editor create a file named phpinfo.2\htdocs Restart Apache. . with the tiny green triangle as shown in Diagram 17 Diagram 17 Simply double click this icon and from the Apache service monitor select 'Restart' as shown in Diagram 18 Diagram 18 18 © Open Source Varsity. It’s the first icon in the image below. All Rights Reserved.php The content of this file is a single line of code: <?php phpinfo(). ?> (type the line exactly as shown) Place this file in: C:\Program Files\Apache Software Foundation\Apache2. Apache’s Icon will be displayed in the Window's task bar.

php file using any browser by typing in the following in its Address bar.php Open Source Varsity The following as shown in Diagram 19 should be displayed in the Browser window. 19 © Open Source Varsity.Execute the phpinfo. All Rights Reserved. .0.1/phpinfo.0. Diagram 19 Apache and PHP have been installed and configured successfully. http://127. This indicates that Apache and PHP are working together just fine.

Make the following changes in the php. Since PHP can be installed anywhere on the computer’s hard disk the value held in the extension_dir needs to be updated. hence all further content will refer to C:\.dll. The Binding Process: In <driveletter>:\php locate a file named libmysql. . All Rights Reserved. NOTE: We have installed PHP in C:\.ini-recommended to php. takes care of each installation process independent of the other. Windows. it’s necessary to inform PHP that MySQL exists. Since PHP application code will be required to access MySQL database resources.S. Copy and paste this file into C:\windows.ini-recommended. Copy and paste this file into C:\windows\system32. 20 © Open Source Varsity. Hence when both products are installed one product really does not know that the other exists. Next in C:\php locate a file php. Change this to: extension_dir = "C:\php\ext" NOTE: This directive needs to hold the path to where the ext folder exists within the PHP installation. libmysql.ini file Locate the extension_dir directive under Paths and Directories as shown in Diagram 20. Rename the file php.Binding PHP to MySQL Introduction: Open Source Varsity The installation processes of PHP and MySQL occurs independent of each other.dll was placed there during the installation process of PHP.ini which in now within C:\windows. The operating system. in this case M. Informing PHP that MySQL exists and configuring PHP to connect to MySQL successfully is known as binding PHP to MySQL.

Please refer to Diagram 2.ini file locate the extension section.dll. This allows the extensions to be located quickly if they have to be commented again for any reason. 21 © Open Source Varsity. This is an updated PHP library required to connect to a MySQL db engine running in memory. .dll php_curl. as shown in Diagram 21 Uncomment these 4 extensions. All Rights Reserved. NOTE: It’s a good idea to copy all four extensions to the very bottom of the extensions section and then uncomment them. by deleting the . creating cookies and a ton of other stuff.dll php_mysql. The php_mysqli. These extensions are required by any internet application delivery framework. Required for connecting to a MySQL db engine running in memory. The extensions are: 1 2 3 4 Extensions php_gd2. Required for URL redirection purposes.dll permits 30% to 40% faster data manipulation than mysql.Open Source Varsity Diagram 20 In the C:\windows\php.dll php_mysqli.dll Represents Permits the compression of images prior their dispatch to a requesting Browser. at the start of the line.

All Rights Reserved.Open Source Varsity Diagram 21 The PHP binding with MySQL is complete along with gd2 and curl. 22 © Open Source Varsity. .

e. Setting the path to the PHP runtime in Windows XP Right click My Computer on the desktop and go to Properties as shown in Diagram 22. All Rights Reserved.My Computer > Properties > Advanced > Environment Variable > System Variable – Path. Diagram 23 23 © Open Source Varsity.exe (i. . ensure that the path php. The System Properties window opens as shown in Diagram 23.at the Command Prompt Open Source Varsity Before you attempt to execute any PHP program. Diagram 22 Then traverse . the PHP runtime) installed on your computer has been registered in the system settings. Click on Edit as shown in Diagram 23 and Diagram 24 and Add C:\php. PATH variable of your computer. as shown in Diagram 25. Here is how you can do this.PHP / MySQL .

Here we will learn how to execute the same PHP files in a command window at the command prompt. 24 © Open Source Varsity.Open Source Varsity Diagram 24 Diagram 25 The System settings in Windows XP are complete.e. Command line PHP script execution provides a quick way of testing PHP codespec without setting up a virtual domain and using a Web server to execute the PHP codespec. This helps you perform "build" and “test” tasks quickly and easily. interaction between PHP and MySQL. we have learnt how to execute a PHP program using a Browser i. . In a previous tutorial. All Rights Reserved.

Diagram 27 NOTE: It is a good practice to keep your MySQL database connection details in a single file for ease of maintenance. It is necessary to connect to MySQL database engine running in memory. All Rights Reserved. Password and exactly where the MySQL db engine is running. Diagram 26 In the studentdetail folder. To be able to do this from within the PHP code spec you must make available a User name. .php will contain the codespec as shown in Example 1 below. In our example we’ve used G:\and within the drive created a folder named studentdetail as shown in Diagram 26. Editplus or your favorite ASCII editor. In this case ‘localhost’ indicates that the MySQL db engine is running on the same computer in which the PHP code snippet is being executed. config.php using any ASCII editor such as Notepad. 25 © Open Source Varsity. Dreamweaver. as shown in Diagram 27.Creating PHP files: Open Source Varsity Creating PHP file For MySQL Connection: Go to any convenient logical drive on your hard disk and create a folder named studentdetail. create a PHP file named config.

Edit Plus or your favorite ASCII editor. includes and evaluates the specified file during the execution of the script. if($con) { echo "Connection Established!".php file once. as shown in Diagram 28.e. The include_once() statement. This is a behavior similar to the include() statement.Example 1 <?php Open Source Varsity $user = "root". createdb. } ?> Copy paste the above codespec into the file named config. $host = "localhost". with the only difference being that if the code from a file has already been included. create a php file named createdb. $con = @mysql_connect($host. As the name suggests.php and save this file in: G:\studentdetail There is a minimum of error handling added to the PHP code within config.). it will be included just once. If the connection to MySQL succeeds. The codespec to include the config. . $pwd) or die("Connection Failed!"). it will not be included again. $user. studentdb. it is necessary to connect to MySQL database engine running in memory. To be able to do this within the PHP code spec include the config. Diagram 28 To create a MySQL database (i.php using any ASCII editor such as Notepad. as given in Example 2. a message “Connection Established!” will be displayed on the Command Window.php will contain the codespec as shown in Example 2 below. All Rights Reserved.php is as given in Example 2. Dreamweaver. $pwd = "passwd".php file in createdb. 26 © Open Source Varsity.php. Creating PHP File for MySQL Database: In the studentdetail folder.

All Rights Reserved. If the connection to MySQL and the database creation succeeds. a message “The MySQL Database Successfully Created” will be displayed on the Command Window. a message “The MySQL Database Creation Failed” will be displayed on the Command Window. .php and save this file in: G:\studentdetail There is a minimum of error handling added to the PHP code within created. If the connection to MySQL database creation fails. This MySQL table will be populated with student data to be manipulated later on demand using PHP codespec. if($result) { echo("The MySQL Database Successfully Created!"). a message “Connection Failed!” will be displayed on the Command Window. Let’s take a look at the structure of the MySQL table StudentMaster: The Structure Of The MySQL StudentMaster Table: Table Definition: Table Name: Column Definition: Column Name StudentID StudentName StudentDegree Speciality DOB StudentMaster Data Type INTEGER VARCHAR VARCHAR VARCHAR VARCHAR Width 3 25 25 35 15 27 © Open Source Varsity.php'). $qry="create database studentdb". it is necessary to populate this empty container with table structure(s) to hold user data which can in turn be manipulated using PHP code.Example 2: <?php include_once('config. $result=mysql_query($qry).studentdb. } else { echo("The MySQL Database Creation Failed!"). which is an empty container.php. The first table we are going to create using PHP code within the MySQL database – studentdb is the table StudentMaster. If the connection to MySQL fails. After creating the MySQL database . } ?> Open Source Varsity Copy paste the above codespec into the file named createdb.

} else { echo("Error creating table"). $query = "CREATE TABLE StudentMaster( StudentID INTEGER(3). All Rights Reserved. Dreamweaver. } ?> 28 © Open Source Varsity. StudentDegree VARCHAR(25). Editplus or your favorite ASCII editor. StudentName VARCHAR(25). ."). Speciality VARCHAR(35). echo "The MySQL Database Connection Established!". Diagram 29 Example 3: <?php include_once('config. $db = "studentdb". mysql_select_db($db) or die("Could not select database!"). as shown in Diagram 29. DOB VARCHAR(15))".Table Description: Column Name StudentID StudentName StudentDegree Speciality DOB Description of column contents Holds an identity number of the student Holds the name of the student Holds the degree acquired by the student Holds the specialty of the student Holds the Date of birth of the student Open Source Varsity Creating PHP File for MySQL Database Table: In the studentdetail folder.php'). if (@mysql_query($query)) { echo("Table created successfully. create a php file named studentmaster.php using any ASCII editor such as Notepad.

” on the Command Window.Copy paste the above codespec into the file named studentmaster. A window pops up. For example: <system prompt> G: [Enter] You will notice that the prompt has now changed.php and save this file in: G:\studentdetail Open Source Varsity If the MySQL connection gets established. There is a minimum of error handling added to the PHP code within studentmaster.php. a message “Connection Established!” will be displayed on the Command Window. go to start -> Run. it will display “Table created successfully. If the database connection is established. type in the path of the file or the drive in which your file is saved and press Enter as shown in Diagram 31. It will then create a table in a MySQL Db engine running in memory. If the query is executed successfully. All Rights Reserved. a message “The MySQL Database Connection Established!” will be displayed on the Command Window. Next. . Executing PHP file at Command Prompt: To run this file on the command prompt. Click on OK as shown in Diagram 30. type in cmd as shown in Diagram 30. Diagram 30 Next. Diagram 31 29 © Open Source Varsity. in ‘Open:’ . This indicates that you are now in G:\ drive.

30 © Open Source Varsity. the output is displayed “Connection Established! The MySQL Database Successfully Created! ”.ext> at the command prompt as shown in Diagram 34.php at the command prompt. you need to run createdb. Diagram 33 In order to create database. Enter php followed by the <filename.php [Enter] As soon as you click on [Enter]. This means that the MySQL database is created successfully.To take a look at the files in this directory at the < System Prompt > enter dir and press Enter. <system prompt> dir [Enter] A list of files is displayed as shown in Diagram 32. All Rights Reserved. <system prompt> php createdb. . NOTE: The dir command displays the files in the current directory. Open Source Varsity Diagram 32 Navigate your prompt where your files are located as shown in Diagram 33.

Diagram 35 Verifying the Database and Table Is Created: Log into MySQL as root with appropriate password as shown in Diagram 36. This means that inside the MySQL database (i.e. All Rights Reserved.ext> at the command prompt. studentmaster) has been created successfully as shown in Diagram 35. ”. As soon as the system prompt changes to mysql> you are ready to verify the database created. you need to run studentmaster.Open Source Varsity Diagram 34 In order to create table(s) inside the database.e.php [Enter] As soon as you click on [Enter]. Diagram 36 Use the SHOW statement to find out what database(s) currently exist on the server as shown in Diagram 37. studentdb). .php at the command prompt. Enter php followed by the <filename. 31 © Open Source Varsity. the output is displayed “Connection Established! The MySQL Database Connection Established! Table created successfully. <system prompt> php studentmaster. the MySQL table (i.

mysql> Show tables. mysql> Use studentdb. Open Source Varsity Diagram 37 MySQL responds and displays a list of all the databases. 32 © Open Source Varsity. which it controls as shown in Diagram 38. All Rights Reserved. Diagram 39 Use the SHOW statement to find out what table(s) currently exists on the database as shown in Diagram 40. . The studentmaster table is successfully created in the database. Diagram 38 Before working with data held within a table it is necessary to inform the MySQL db engine which database holds the tables. The USE command specifies which database the MySQL db engine should set as active as shown in Diagram 39. The studentdb database is successfully created.mysql> Show databases.

Open Source Varsity Diagram 40 33 © Open Source Varsity. All Rights Reserved. .

support for statements that have been previously prepared. it is not required that MySQL be shipped with GUI tools in order to administer the databases or manage the data therein. most free software projects that must have a full featured database management system (or DMS) use MySQL. It is possible for users to use a command line tool or download MySQL Frontends from a variety of parties that have the software necessary and web applications to manage the databases. an enhanced level of debugging support. Each one of these websites use MySQL for storage and the logging of user data. Google. MySQLi Extension (or simply known as MySQL Improved or MySQLi) is a relational database driver that is used mainly in the PHP programming language. Its use can be seen widely in such popular web sites as Flickr. This RDMS runs as its own server and provides multi-user access to multiple databases at once. and YouTube. which was simply a means to manage databases over servers. and work with the data records. It is quite literally an improved version of its predecessor. As an RDBMS. Nokia. and Microsoft Windows. Members of the MySQL community have created many different branches of the RDMS –the most popular of which are Drizzle and MariaDB. There are a few that are more prominent than others. The MySQLi extension comes equipped with many benefits that compliment as well as improve those that were provided by its predecessor. 34 © Open Source Varsity. build the databases. The code is comprised of the C and C++ languages and uses many different system platforms –including Linux. MySQL can be found in many web applications as the database component of a solution bundle (or LAMP) software stack. MySQL. Wikipedia. Mac OS X. The source code of MySQL is available under the terms set forth in the GNU General Public License as well as a plethora of proprietary agreements. As well as being the prototype of several branches. All Rights Reserved. MySQL. It provides an interface to the already founded MySQL databases. These features that are meant to enhance the functionality of the MySQL (as well as provide an update to the database manager as a whole) are an object oriented interface. FaceBook. and an enhanced level of server support that is already embedded in the infrastructure of the database. support for a variety of statements.MySQL and MySQLi Open Source Varsity MySQL is a relational database management system (or RDBMS) –meaning that it is a database management system based on the relational model. support for any kind of transaction that takes place. .