Skip to content

Instantly share code, notes, and snippets.

@RSquaredSoftware
Created April 12, 2012 01:37
Show Gist options
  • Save RSquaredSoftware/2364129 to your computer and use it in GitHub Desktop.
Save RSquaredSoftware/2364129 to your computer and use it in GitHub Desktop.
<?php
//create a PDO object to interface with the database
try {
$dbh = new PDO('mysql:localhost:3306', 'root', '');
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
//create the database
try{
$dbh->exec("CREATE DATABASE IF NOT EXISTS isp5;");
}
catch (PDOException $e) {
die("Creating Database Error: ". $e->getMessage());
}
//create the songs table
try{
$dbh->exec("USE isp5;");
$dbh->exec("CREATE TABLE IF NOT EXISTS songs ( song VARCHAR(40) NOT NULL, artist VARCHAR(40), votes INT(10) NOT NULL, PRIMARY KEY (song));");
}
catch (PDOException $e) {
die("Creating Table Error: ". $e->getMessage());
}
//put a few test songs in the database
try{
$dbh->exec("USE isp5;");
$dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song1', 'artist1', '0');");
$dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song2', 'artist1', '0');");
$dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song3', 'artist2', '0');");
}
catch (PDOException $e) {
die("Adding Test Entries: ". $e->getMessage());
}
echo "<html><head> <link href='common/style.css' rel=stylesheet type=text/css /> </head><body>";
//create the songs table
try{
$dbh->exec("USE isp5;");
$sth = $dbh->prepare("SELECT * FROM songs");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
echo "<table>";
foreach($result as $r){
echo "<tr><td>".$r[song]."</td><td>".$r[artist]."</td><td>".$r[votes]."</td>";
echo "<td><a href='#' name=$r[song] onclick='upvote($r[song]);'><img src='common/up.png' /></a>";
echo "<a href='#' name=$r[song] onclick='downvote($r[song]);'><img src='common/down.png' /></a></td></tr>";
}
echo "</table>";
}
catch (PDOException $e) {
die("Printing Data Error: ". $e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment