PHP Tutorial

PHP Tutorial #1

PHP is one of the new breed of languages designed to make database access across the Internet that much easier. However, as it is based on the language C it is also capable of running as a standalone programming language as well but we will be concentrating on the Internet aspects. PHP is an acronym (its actually a recursive acronym beloved of the Unix community) that stands for PHP Hypertext Preprocessor which probably doesn't mean much to you particularly if you have no experience of using programming languages particuarly on the UNIX/GNU Linux platform. Fear not, this tutorial will start from the beginning and will assume no previous experience on your part although a knowledge of a programming language and HTML would help. On with the show then...

PHP has to be installed onto the target computer before it can be used and is available for both Windows and Linux. As I found it difficult to install the Windows version, I'll be concentrating on the Linux version. The backend database being used is MySQL which is also available on both platforms. Finally, a web server such as Apache or one of the MS offerings will be required. If you use Linux then chances are this will already be installed by default. For the purposes of this tutorial, I shall assume that Linux is being used which in my case is Mandrake 9.x through to 10.x as I couldn't get MySQL to work on SUSE 8.2 which was to say the least frustrating. Mandrake has a very nice installer which makes it easy to check if all the packages required are installed which for the sake of clarity are:-

PHP
MySQL
Apache

To test they are all installed, open your favourite browser (I use Mozilla but Konqueror will do just as well) and enter the following address:-

http://localhost

If Apache is working then you will get a nice Welcome screen. If you get an error then chances are Apache isn't installed. To check if PHP is running (Apache has to be installed first), create a new file in KEdit (or whatever editor you use) and type in the following:-

<? PHP phpinfo(); ?>

and save it with the name phptest.php into the correct folder for Apache to find which in the case of Mandrake is:-

/var/www/html/

so the complete path to save to will be:-

/var/www/html/phptest.php

If you call it phptest.htm (as I did in the first draft of this tutorial doh!) then nothing will happen. You can then call it up using your browser using the address:-

http://localhost/phptest.htm

If you get a nice test screen then all is well but if you get an error then you probably need to install PHP. Sorry to the more experienced of you if all this seems a bit basic but it is best to be sure that everyone reading this can get it all working first before we get onto the more interesting stuff. Sp now we should have Apache and PHP installed and running. Finally, you need MySQL. To check if that is working then try creating another file (called say testmysql.htm) and enter this text:-

<html>
  <head>
    <title>MYSQL Connection Test</title>
  </head>
  <body>
  <H2>
  <?php
	$connection = mysql_connect("localhost","root","")
	    or die("Sorry - unable to connect");
	echo("Congratulations - you are connected");
  ?>
  </H2>
  </body>
</html>

Don't worry too much about what it all means. Once again, go to the browser and put in the following:-

http://localhost/phptest.htm

as an address and you should a message saying you are connected. If you don't then either MySQL isn't installed or it isn't running. To get it running on my system requires the following command to be entered into a terminal session:-

/usr/share/mysql/mysql.server start

and you should get a message saying that it has started. Entering the command:-

mysql

should elicit a prompt:-

mysql>

and you can enter either help or quit to get help or to quit the program. If you get an error then either it is the wrong path or like me you are using SUSE 8.2 and can't get it to work (and no one can tell me why it doesn't work). If all is well then refresh the browser page and you should get the message saying you are connected. So much for getting it all running. Now we can get down to some coding. Take a look at this again:-

<? PHP phpinfo(); ?>

which I have taken from the first file we created above. How does it work? It looks a little cryptic but is quite straightforward to understand. Now there is something that you need to understand about PHP. PHP is a Server side language which means that it runs on the server, not in the browser. All the browser has to do is display what the server sends to it. Don't worry if you don't get it at the moment, all you really have to know is that the file must be saved on the server otherwise it won't work. So what about those cryptic letters above? First, the server needs to know what the file contains before it can interpret it. If it found <HTML> then it would leave it up to the browser to render it. However, it says PHP which means that the server must use the PHP program to interpret it. The sharp brackets (the < and >) tell it to expect some scripting and the question marks delimit the beginning and end. Inside we have the word PHP so the server knows it is a PHP program. The word phpinfo is a special word that PHP knows how to deal with which in this case is to display some information about its version number and so on. It is important that the brackets () are there because it is part of the command (more about those later) and the semi-colon ; is there to show that the line has ended. So really, it isn't as bad as it first looks. Now to that other file which looked like this:-

<html>
  <head>
    <title>MYSQL Connection Test</title>
  </head>
  <body>
  <H2>
  <?php
	$connection = mysql_connect("localhost","root","")
	or die("Sorry - unable to connect");
	echo("Congratulations - you are connected");
  ?>
  </H2>
  </body>
</html>

This breaks down as follows:-

<html>

This is the start of an HTML page. Now hold on I can hear you saying, whats all this about HTML? I thought this was a server side langugage we were learning. Well so it is but in order to write text to the browser window, we need to use HTML so that we can mix HTML and data from the server to give a rich and interactive experience for the user. You will see how this can be used to great effect in future tutorials. So back to the file.

  <head>
    <title>MYSQL Connection Test</title>
  </head>
  <body>

So far it is standard HTML with a title that will show in the browser caption at the top of the window and as there isn't anything else to go into the head of the page, it is closed off with </head>.

<H1>

This is the start of a heading showing that anything that follows before we reach the closing </H1> will be in large bold text.

  <?php
	$connection = mysql_connect("localhost","root","")
	      or die("Sorry - unable to connect");
	echo("Congratulations - you are connected");
  ?>

Note the layout of the text with the by now familiar <?php (PHP isn't case sensitive) which helps us to make sense of what is going on in the code sections. $connection is a variable (which ARE case sensitive but more on those later) that holds the result of the connection to the MySQL database engine. die ill kill the program if there is an error and send a text message to the user via the browser window that an error has occurred. While developing and learning PHP, it will be helpful to send text to the browser window to show which parts of the code have ben called and this is done using echo which reports that all is well. Don't worry too much on what all this is doing as it will all be covered in future tutorials. I think that just about wraps it up for the moment. If you are having difficulty with any of this then please send me an email and I'll try to help. Look out for the next tutorial coming soon...

Back

Written by Nick Cheesman. Last updated: 01/04/2004
Please eMail me at:
nickjc@nickjc.co.uk