Home
   About Me
   Rene's Resume
   Presentations
   Services
   Rates & Contracts
   Portfolio / Clients
   Demo Apps
   White Papers & Tips
   Tutorials
   Contact Me
 

Starting Out

The first thing we need to do is verify that the two major components of this tutorial are working correctly. If you do this now, it will rule out a whole host of potential errors when you are attempting to debug the minor scripts written in this tutorial.

2.1 "Hello World" in PHP

"Hello World" has got to be one of the most maligned programs in the world. (Aside from various Microsoft's products.) It is exceedingly simplistic to write and does little. What it does prove however is that the basic workings of PHP are up and running.

Create a file called hello.php and type in the following:

    <html>
    <body>
    <?
        echo("Hello World\n");
    ?>
    </body>
    </html>
    

When you view that file on your web server, you should see a single line saying: Hello World. If you do, then PHP is working correctly working.

2.2 Creating a test table in MySQL

I'm assuming that you have telnet access to a machine which can connect to the MySQL server. If you do not, it is still possible to create and manipulate database on a remote server through Perl and the DBI interface, however that is outside the bounds of this small tutorial. If you're looking for a web interface to maintain or administer MySQL, I recommend you take a look at phpMyAdmin

Connect to your MySQL database server and create a test database and the test table that we'll use for this tutorial.

    %> mysql -uUSERNAME -pPASSWORD
    
    mysql> create database phptest;
    Query OK, 1 row affected (0.13 sec)
    
    mysql> use phptest;
    Database changed
    
    mysql> create table TEST
        -> (
        -> ID        int auto_increment primary key,
        -> Name      varchar(32),
        -> Age       int,
        -> Salary    int
        -> );
    Query OK, 0 rows affected (0.11 sec)
    

Using varchar instead of char is recommended unless you are going to restrict the length of the strings entered by the user in HTML forms. The 32 in the table definition sets the maximum size the string can be. Columns of type varchar can be up to 255 characters long.

2.3 Adding information to the test table

Now we'll add a bunch of rows to the table so we'll have something to play with when we try to access this database via PHP.

    mysql> insert into TEST values (0,'Billy Bob Barker','87','93000');
    Query OK, 1 row affected (0.09 sec)
    
    mysql> insert into TEST values (0,'Sniffling Sam Sloth','23','12000');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> insert into TEST values (0,'Roy Bartley','31','87000');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> insert into TEST values (0,'Leroy Longrad','45','63000');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> insert into TEST values (0,'Amy Antwerp','37','34000');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> insert into TEST values (0,'Kim Kruger','57','76000');
    Query OK, 1 row affected (0.01 sec)
    

The resulting table data should look like:

    mysql> select * from TEST;
    +----+---------------------+------+--------+
    | ID | Name                | Age  | Salary |
    +----+---------------------+------+--------+
    |  1 | Billy Bob Barker    |   87 |  93000 |
    |  2 | Sniffling Sam Sloth |   23 |  12000 |
    |  3 | Roy Bartley         |   31 |  87000 |
    |  4 | Leroy Longrad       |   45 |  63000 |
    |  5 | Amy Antwerp         |   37 |  34000 |
    |  6 | Kim Kruger          |   57 |  76000 |
    +----+---------------------+------+--------+
    6 rows in set (0.16 sec)
    

2.4 Connecting to the database with PHP

Now that we know that PHP is working and there is information ready and waiting in the database, it's time to tie them both together. Save the PHP class code to a file called "util.php" or something equally meaningful in the directory where the rest of your PHP files will reside. Edit this file and update the username and password which you will be using to connect to the database.

Create a new file, "testdb.php" and type in the following:

    <html>
    <body>
    <?
        require("util.php");
        $sql = new MySQL_class;
        $sql->Create("phptest");
    
        echo("Database connection successful.\n");
    ?>
    </body>
    </html>
    

Loading this file into your browser, you should see Database connection successful. If you do, then you've contacted the database, logged in with the right password and are ready to roll!

 

This page was generated in 0.0008 seconds.
Website content © 2024