cheap nfl jerseys china cheap nfl jerseys free shipping wholesale nfl jerseys china wholesale jerseys from china cheap nfl jerseys free shipping cheap nfl jerseys for sale cheap jerseys free shipping wholesale nfl jerseys from china cheap nfl jerseys sale cheap nike nfl jerseys china wholesale jerseys free shipping cheap nfl jerseys wholesale wholesale nfl jerseys online cheap nfl jerseys wholesale china jerseys wholesale cheap coach handbags outlet authentic designer handbags cheap coach handbags outlet cheap coach purses outlet discount coach bags coach bags sale coach purse outlet cheap real coach purses coach handbags sale online coach purse outlet michael kors outlet online store cheap michael kors bags cheap michael kors purse michael kors factory outlet online cheap michael kors handbags cheap michael kors purses michael kors bags outlet online cheap michael kors purse michael kors handbags discount
MySQL & PHP

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.php3 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.

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> 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.

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.php3" 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.php3" and type in the following:

    <html>
    <body>
    <?
        require("util.php3");
        $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!

Back Index Next Chapter

Vt. Web Wizard Home    |     Email Questions & Comments

© 1998 Vt. Web Wizard