Random Quote Generator

This entry was posted by Matt Kendrick on Thursday, 27 August, 2009 at

Here’s a quick script I wrote to display random quotes.


<?php

//Random Quotes Script
//Matt Kendrick – Aug. 2009
//mattkendrick.com

//Displays random quotes from a MySQL Table

//database server
$db_server = “localhost”;

//database user
$db_user = “user”;

//database password
$db_pass = “password”;

//database name
$database = “quote_db”;

//connect to database
mysql_connect($db_server,$db_user,$db_pass);

//select database
mysql_select_db($database);

//query database for random quote
$sql = “SELECT quote_text,author FROM quotes ORDER BY RAND() LIMIT 1″;
$result = mysql_query($sql);

while($row = mysql_fetch_array($result,MYSQL_NUM))
{

//display result – format however you like
echo “<i>’$row[0]‘</i> – <b>$row[1]</b>”;

}

//close database connection
mysql_close();

?>

Below is the table structure needed.


– Table structure for table `quotes`

CREATE TABLE IF NOT EXISTS `quotes` (
`quote_id` int(11) NOT NULL auto_increment,
`quote_text` text NOT NULL,
`author` varchar(50) NOT NULL,
PRIMARY KEY (`quote_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


– Dumping data for table `quotes`

INSERT INTO `quotes` (`quote_id`, `quote_text`, `author`) VALUES
(1, ‘I think it’’s fair to say that personal computers have become the most empowering tool we”ve ever created. They”re tools of communication, they”re tools of creativity, and they can be shaped by their user.’, ‘Bill Gates’),
(2, ‘Never trust a computer you can”t throw out a window.’, ‘Steve Wozniak’),
(3, ‘The Internet is not just one thing, it’’s a collection of things – of numerous communications networks that all speak the same digital language. ‘, ‘Jim Clark’),
(4, ‘What do we want our kids to do? Sweep up around Japanese computers?’, ‘Walter F. Mondale’);

You can download the complete script and database table here: Random Quote Generator (86)

Comments are closed.

 

You need to log in to vote

The blog owner requires users to be logged in to be able to vote for this post.

Alternatively, if you do not have an account yet you can create one here.

Powered by Vote It Up