background
|
|
Computer

Computer
|
|
Up Image
Navigation
Search this Site
Enter your search terms

Site Breadcrumb - You are here
|
PHP/MySQL Tutorial
Lesson 1

by Graeme Merrall

Page 5 — Load Up a Database

So now we're ready to plug in MySQL. One handy way of knowing what options are available in PHP and what's going on in your server is to use the phpinfo() function. Create a script with the following:


<html>

<body>



<?php

phpinfo();

?>



</body>

</html>

Save and view this script through your Web server. You'll see a page filled with useful and interesting information like this. This info tells all about your server, internal Web server environment variables, the options that are compiled, and on and on. In the first section, Extensions, look for a line beginning with MySQL. If this is missing, then for some reason MySQL hasn't made it into PHP. Go back and review the installation steps and check the PHP documentation to see if you missed anything.

If MySQL is there, then you're ready to roll.

Before we can get data out of MySQL, we have to put data in it. There's really no easy way to do it at this stage. Most PHP scripts come with what's known as a dump file that contains all the data required to create and populate a MySQL database. The ins and outs of this process are really outside the scope of this tutorial, so I'll just do it for you.

MySQL uses its own user permissions table. At setup, a default user (root) is automatically created with no password. It's up to the database administrator to add other users with various permissions, but I could write a whole other article on that, so we'll stick with using the root user. If you set up your own server and database, it's vital that you assign a password to the root user.

Anyway, let's get on with the database. For Win32 users, I'm sorry, but this requires some DOS work. You'll have to use a DOS window or type everything in the Run window. Don't forget to type in the path to the location of the MySQL/bin directory with your commands. Unix users can work from the MySQL bin directory, but you may have to start each command with ./ so the programs run.

The first thing we need to do is create the actual database. From the command line, type:

mysqladmin -u root create mydb

That creates a database called "mydb." The flag tells MySQL that we're doing this as the root user.

Next we'll add some data using everyone's favorite example, the employees database. We're going to need that dump file I mentioned earlier. If you're interested in how it goes together, review the manual that comes with MySQL or check out http://www.turbolift.com/mysql/.

Copy and paste the following text to a file and save it in MySQL's bin directory. (I'll call the file mydb.dump.)


CREATE TABLE employees (  id tinyint(4) DEFAULT '0' NOT NULL AUTO_INCREMENT,  first varchar(20),  last varchar(20),  address varchar(255),  position varchar(50),  PRIMARY KEY (id),  UNIQUE id (id));INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St, Cityname','Marketing Manager');

INSERT INTO employees VALUES (2,'John','Roberts','45 There St , Townville','Telephonist');

INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman');

If the lines wrap, make sure that each insert statement is on a new line. Now we'll insert it into the mydb database. From the command line, type:


mysql -u root mydb < mydb.dump

You shouldn't get any errors doing this. If you do, check for incorrect line wrapping.

next page»