1. Home
  2. Computing & Technology
  3. PHP / MySQL

Simple PHP & MySQL Poll

By Angela Bradley, About.com

2 of 5

Voting Script - Part 1

// Connects to your Database
mysql_connect("your_server", "your_login", "your_pass") or die(mysql_error());
mysql_select_db("your_database") or die(mysql_error());

//Name of our cookie
$cookie = "Voted";

//A function to display our results - this refrences vote_pie.php which we will also make
function pie ()
{
$data = mysql_query("SELECT * FROM votes")
or die(mysql_error());
$result = mysql_fetch_array( $data );
$total = $result[first] + $result[sec] + $result[third];
$one = round (360 * $result[first] / $total);
$two = round (360 * $result[sec] / $total);
$per1 = round ($result[first] / $total * 100);
$per2 = round ($result[sec] / $total * 100);
$per3 = round ($result[third] / $total * 100);
echo "<img src=vote_pie.php?one=".$one."&two=".$two."><br>";
Echo "<font color=ff0000>FIRST</font> = $result[first] votes, $per1 %<br>
<font color=0000ff>SECOND </font> = $result[sec] votes, $per2 %<br>
<font color=00ff00>THIRD </font> = $result[third] votes, $per3 %<br>";
}
We start out or script with the information we need to connect to our database. We then name our cookie, and define a function called pie. In our pie function we retrieve the data from our database. We also preform a few calculations that help us display the results in a user friendly way, such as the percentage each vote has, and how many degrees out of 360 that percentage makes up. We reference vote_pie.php which we will create later in the tutorial.
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Graphics- GD Library
  5. Simple PHP Poll - Graphical PHP Poll - PHP Poll Tutorial

©2009 About.com, a part of The New York Times Company.

All rights reserved.