Specifications

Graphing is the other thing these functions are primarily used for. You can chart any data you
wantsales, Web hits, or whatever takes your fancy.
For this example, we have spent a few minutes setting up a MySQL database called poll. It
contains one table called poll_results, which holds the candidates names in the candidate col-
umn, and the number of votes they have received in the num_votes column. We have also cre-
ated a user for this database called poll, with password poll. This takes about five minutes to
set up, and you can do this by running the SQL script shown in Listing 19.3. You can do this
piping the script through a root login using
mysql -u root -p < pollsetup.sql
Of course, you could also use the login of any user with the appropriate MySQL privileges.
LISTING 19.3 pollsetup.sql Setting Up the Poll Database
create database poll;
use poll;
create table poll_results (
candidate varchar(30),
num_votes int
);
insert into poll_results values
(‘John Smith’, 0),
(‘Mary Jones’, 0),
(‘Fred Bloggs’, 0)
;
grant all privileges
on poll.*
to poll@localhost
identified by ‘poll’;
This database contains three candidates. We provide a voting interface via a page called
vote.html. The code for this page is shown in Listing 19.4.
LISTING 19.4 vote.htmlUsers Can Cast Their Votes Here
<html>
<head>
<title>Polling</title>
<head>
<body>
<h1>Pop Poll</h1>
<p>Who will you vote for in the election?</p>
<form method=post action=”show_poll.php”>
Advanced PHP Techniques
P
ART IV
420
24 7842 CH19 3/6/01 3:42 PM Page 420