From 68e0fb630b33c627810a66701748cbfeb7e9c2cc Mon Sep 17 00:00:00 2001 From: Ian Weller Date: Sat, 29 Mar 2008 20:59:47 +0000 Subject: [PATCH] - Solved issue 2; now using PDO --- README | 4 ++++ all.php | 4 ++-- config.php | 12 ++++++++++-- header.php | 4 ++-- index.php | 5 +++-- latest.php | 4 ++-- quote.php | 6 +++--- submit.php | 7 +++---- submitbulk.php | 9 +++++---- 9 files changed, 34 insertions(+), 21 deletions(-) diff --git a/README b/README index 5ba761b..e0f4833 100644 --- a/README +++ b/README @@ -20,3 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., This is very immature code. It may lash out at you like a teenager. Feed it well (write patches) and report to the developer, please! +http://miniqdb.googlecode.com/ + +miniqdb requires the PDO database abstraction layer. This comes with PHP 5.1 or +later, and is available as a PECL extension for PHP 5.0. diff --git a/all.php b/all.php index c273b3c..c324d04 100644 --- a/all.php +++ b/all.php @@ -19,9 +19,9 @@ require "header.php"; -$result = mysql_query("SELECT id,quote FROM miniqdb ORDER BY id ASC", $conn); +$st = $db->query("SELECT id,quote FROM miniqdb ORDER BY id ASC"); -while ($r = mysql_fetch_assoc($result)) { +foreach ($st->fetchAll() as $r) { echo '
'; echo '
#' . $r['id'] . "\n";
 	echo $r['quote'];
diff --git a/config.php b/config.php
index a329ecb..fdb72bd 100644
--- a/config.php
+++ b/config.php
@@ -17,16 +17,24 @@
     with this program; if not, write to the Free Software Foundation, Inc.,
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
 
+///////////////
 // mysql config
 $user = '';
 $pass = '';
 $db = '';
+// use localhost as the host and keep port as null if mysql is local
 $host = '';
+$port = null;
 // qdb config
 $qdbname = ''; // shown at top of every page
 
+////////////////////
 // code, don't touch
-$conn = mysql_connect("$host","$user","$pass");
-mysql_select_db ($db, $conn);
+$pdostring = 'mysql:host='.$host;
+if ($port != null) {
+	$pdostring .= ';' . $port;
+}
+$pdostring .= ";dbname=$db";
+$db = new PDO($pdostring, $user, $pass);
 
 ?>
diff --git a/header.php b/header.php
index 5bf12c3..5cc836b 100644
--- a/header.php
+++ b/header.php
@@ -19,8 +19,8 @@
 
 require 'config.php';
 
-$totalq = mysql_query("SELECT * FROM miniqdb", $conn);
-$numq = mysql_num_rows($totalq);
+$totalq = $db->query("SELECT COUNT(*) FROM miniqdb");
+$numq = $totalq->fetchColumn(0);
 
 echo '';
 echo '';
diff --git a/index.php b/index.php
index aa39986..e418f14 100644
--- a/index.php
+++ b/index.php
@@ -20,11 +20,12 @@
 require "header.php";
 echo "

displaying random 20 or so quotes

"; +// we take '20 or so' literally $limit = mt_rand(18, 22); -$result = mysql_query("SELECT id,quote FROM miniqdb ORDER BY RAND() LIMIT $limit", $conn); +$st = $db->query("SELECT id,quote FROM miniqdb ORDER BY RAND() LIMIT $limit"); -while ($r = mysql_fetch_assoc($result)) { +foreach ($st->fetchAll() as $r) { echo '
'; echo '
#' . $r['id'] . "\n";
 	echo $r['quote'];
diff --git a/latest.php b/latest.php
index 6c58aa3..da1780c 100644
--- a/latest.php
+++ b/latest.php
@@ -19,9 +19,9 @@
 
 require "header.php";
 
-$result = mysql_query("SELECT id,quote FROM miniqdb ORDER BY id DESC LIMIT 10", $conn);
+$st = $db->query("SELECT id,quote FROM miniqdb ORDER BY id DESC LIMIT 10");
 
-while ($r = mysql_fetch_assoc($result)) {
+foreach ($st->fetchAll() as $r) {
 	echo '
'; echo '
#' . $r['id'] . "\n";
 	echo $r['quote'];
diff --git a/quote.php b/quote.php
index 81d692d..47ed718 100644
--- a/quote.php
+++ b/quote.php
@@ -25,11 +25,11 @@ $id = $_GET['id'];
 
 require "header.php";
 
-$result = mysql_query("SELECT id,quote FROM miniqdb WHERE id=$id", $conn);
-if (mysql_num_rows($result) == 0) {
+$st = $db->query("SELECT id,quote FROM miniqdb WHERE id=$id");
+if (!$st) {
 	echo "

Quote $id doesn't exist.

"; } else { - while ($r = mysql_fetch_assoc($result)) { + foreach ($st->fetchAll() as $r) { echo '
'; echo '
#' . $r['id'] . "\n";
 		echo $r['quote'];
diff --git a/submit.php b/submit.php
index e3be334..e17c2e1 100644
--- a/submit.php
+++ b/submit.php
@@ -21,7 +21,6 @@ require "header.php";
 
 // Gets args from POST
 $quote = $_POST["quote"];
-$epoch = date("U");
 
 // Replace IRC "<" and ">" characters with the HTML equivalent.
 // Then strip newlines from the top and bottom of the quote.
@@ -32,9 +31,9 @@ $quote_lb = trim($quote_gt);
 // Insert into database as new. We leave out ID number cause the
 // database will autoincrement that field by itself.
 
-$sql = "INSERT INTO miniqdb (epoch,quote) VALUES ('$epoch','$quote_lb')";
-$result = mysql_query($sql);
-$id = mysql_insert_id();
+$st = $db->prepare('INSERT INTO miniqdb (epoch,quote) VALUES (?,?)');
+$st->execute(array(date('U'), $quote_lb));
+$id = $db->lastInsertId();
 
 echo "

quote posted

"; echo "

Quote $id was just added.

"; diff --git a/submitbulk.php b/submitbulk.php index a6add41..6c02eed 100644 --- a/submitbulk.php +++ b/submitbulk.php @@ -21,11 +21,12 @@ require "header.php"; // Gets args from POST $quote = $_POST["quote"]; -$epoch = date("U"); // Explode that text. $kaboom = explode("\r\n\r\n", $quote); +$ids = array(); + foreach($kaboom as $quote) { global $ids; @@ -39,9 +40,9 @@ foreach($kaboom as $quote) { // Insert into database as new. We leave out ID number cause the // database will autoincrement that field by itself. - $sql = "INSERT INTO miniqdb (epoch,quote) VALUES ('$epoch','$quote_lb')"; - $result = mysql_query($sql); - $ids[] = mysql_insert_id(); + $st = $db->prepare('INSERT INTO miniqdb (epoch,quote) VALUES (?,?)'); + $st->execute(array(date('U'), $quote_lb)); + $ids[] = $db->lastInsertId(); } -- 2.11.4.GIT