The sample shop (which comes as a gzipped tar archive) contains two files with .sql extensions. If you have access to MySQL on your server, you can feed those files to it and create the required tables to run SimpleshoPHP. There are even a half-dozen or so books so you can see what it looks like when you order something.
Of course, that does assume you have MySQL installed.
Installing it isn't that big a deal on a typical Linux box. The first time you start up the mysqld server, in fact, the startup script will create your first database for you!
By default, the database it creates will be called "mysql" and that is where all the information about your other databases will be stored. It also starts out completely unprotected -- simply by running the mysql client program as "root" you have complete access to that database, and can create and delete databases at will.
That means you really ought to assign a password to your mysql database, but first let's take advantage of the situation to create our new database.
The MySQL server program, mysqld, has to be running:
# /etc/init.d/mysqld start
As long as the server is running, you can connect to it with client programs, such as mysql or the web-based client phpMyAdmin. The next step is to create our own database, a new one, which will contain the two tables used by our used book shop.
# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2218 to server version: 3.23.54 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
That "mysql> " is a command prompt. It means the mysql client is waiting for instructions. Let's give it one:
mysql> create database bookstore; Query OK, 1 row affected (0.05 sec) mysql>
Painless, wasn't it? Note that our command ended with a semicolon. If we had just typed
mysql> create database bookstore >
...note that we get a different prompt. MySQL thinks our command is not complete, and is waiting for us to continue it on a second line. To tell it you're done, just type a semicolon and press Enter again.
So, we have an (empty) database called "bookstore." We could create our tables by typing in the description at the mysql prompt, but since I've already prepared a file, here's how to use it. Exit from mysql:
mysql> quit Bye
(How polite!) Now run mysql again, but this time we're going to tell it that we want it to use the bookstore database, and that we want it to read in a text file of mysql commands. You do that by using the Unix input redirection operator, < like so:
# mysql -u root bookstore < books.sql # mysql -u root bookstore < bag.sql
The result is that the bookstore database now contains two tables, books and bag. The books table actually contains a few rows of data, while bag is just an empty table description. Our store will write to it and erase from it as needed.
In case you're curious, here's what the "bag.sql" file looks like:
# phpMyAdmin MySQL-Dump # version 2.4.0 # http://www.phpmyadmin.net/ (download page) # # Host: localhost # Generation Time: May 04, 2003 at 08:57 PM # Server version: 3.23.54 # PHP Version: 4.1.2 # Database : `bookstore` # -------------------------------------------------------- # # Table structure for table `bag` # CREATE TABLE bag ( sess varchar(32) NOT NULL default '', sku varchar(16) NOT NULL default '', qty tinyint(4) NOT NULL default '0' ) TYPE=MyISAM;
The lines that begin with "#" are just comments. If you were to type the remaining lines into the mysql program's command line, it would work exactly the same as reading the file with the < operator. (Remember, we told it which database to use -- "bookstore" -- before we started reading the file.) Here's an example of creating a table and also loading up some data:
# phpMyAdmin MySQL-Dump
# version 2.4.0
# http://www.phpmyadmin.net/ (download page)
#
# Host: localhost
# Generation Time: May 04, 2003 at 08:55 PM
# Server version: 3.23.54
# PHP Version: 4.1.2
# Database : `bookstore`
# --------------------------------------------------------
#
# Table structure for table `books`
#
CREATE TABLE books (
TITLE varchar(96) NOT NULL default '',
AUTHOR varchar(64) NOT NULL default '',
ISBN varchar(16) default NULL,
PUBLISHER varchar(64) default NULL,
FORMAT varchar(16) default NULL,
CONDITION varchar(4) default NULL,
SKU varchar(16) NOT NULL default '0',
DEPT varchar(16) NOT NULL default '',
PRICE float NOT NULL default '3',
STOCK tinyint(4) NOT NULL default '1',
PRIMARY KEY (SKU),
KEY AUTHOR (AUTHOR),
KEY TITLE (TITLE),
KEY DEPT (DEPT)
) TYPE=MyISAM;
#
# Dumping data for table `books`
#
INSERT INTO books VALUES ('TITLE', 'AUTHOR', 'ISBN', 'PUBLISHER',
'BOOKFORMAT', 'COND', 'SKU', 'LOCATION', '0', 0);
INSERT INTO books VALUES ('Addams and Evil', 'Addams, Charles', '', 'Cardinal',
'Paperback', 'Average', 'P34AdC20', 'Vintage Pbks', '3', 1);
INSERT INTO books VALUES ('Murder Game', 'Adler, Bill', '', 'Carroll & Graf',
'Hardcover', 'Like New', 'H34AdB30', 'Oddities', '3', 1);
We used phpMyAdmin to make our table a little fancier this time. For one thing, we have defined multiple keys. MySQL will maintain indexes for each key column which means that we can search the entire table for a value in one of those columns much more quickly than if we had to scan the table from top to bottom. We can do a quick search for a title, or an author, or all entries within a department, which pretty much covers our needs. We can still scan the entire table if we have to -- if you wanted all books priced under six dollars, that's how MySQL would find them.
Here's a little trick -- notice that first line? It gives the layout of our table. But it's not a book, and we certainly don't want it cluttering up our customers' screens. Do we go in and erase it? It might come in handy, in case we need a reminder of our table layout. Well, wait a second -- if the value of "stock" is set to zero, that means our browse page will never show it, so we don't have to delete it.
There's one more task we need to perform. Remember "MyUsername" and "MyPassword" from our PHP code? That name and that password don't exist yet. So, let's hop back into mysql for a moment:
# mysql -u root mysql mysql> grant all on bookstore.* to MyUsername@localhost identified by 'MyPassword'; Query OK, 0 rows affected (0.17 sec)
That's all you need to run the demo store as documented here. I messed up a bit on the previous edition of this page -- you have to specify "all tables in the bookstore database" by saying "bookstore.*", not just "bookstore" -- and you probably have to specify the server as well as the username, by saying "MyUsername@localhost" rather than just "MyUsername".
The downloadable source code uses a different name ("roboclerk") and password -- sorry, but that was the best way to make sure you find and fix all the references to them. At least we put those common variables into a single "include" file this time, so you only have to change them in one place!
Really populating the books table with a practical number of entries needs more of a power tool. I can't say enough good things about phpMyAdmin, another Free Software tool available from SourceForge. It provides a web-based view of your databases and tables, and allows you to easily import an entire spreadsheet file. I can feed 500 books into my books table in a second.
Next -- and last -- we'll securely email orders by using GPG.
|
|
|
You are invited to post comments or questions on the SimpleshoPHP forum at SourceForge.net.