Random Quote generation from a mySQL database. Works properly some of the other ones I've seen here didn't work properly.
By : sebexist
<?php /* RANDOM QUOTE FORM mySQL DATABASE CREATED BY: SEBASTIAN M CREATED DATE: 08.15.03 PART OF WEBEXIST INTERACTIVE webmaster@webexist.com */
$host = ""; //HOST NAME $user = ""; //DATABASE USER NAME $pass = ""; //DATABASE USER PASSWORD $db = ""; //DATABASE NAME $table = ""; //DATABASE TABLE NAME $column = ""; //COLUMN NAME
//DO NOT MAKE ANY CHANGES BELOW mysql_connect($host, $user, $pass) or die("Couldnt connect to database"); mysql_select_db($db) or die("Couldnt select database"); $result = mysql_query("SELECT * FROM ".$table.""); $num_results = mysql_num_rows($result);
$random = rand(0, ($num_results - 1)); //GET A RANDOM NUMBER FROM 0 TO THE TOTAL ROWS MINUS 1
for ($i=0; $i<$num_results; $i++) //GO THROUGH THE DATABASE { $row = mysql_fetch_array($result); if($i == $random) // IF THE $i EQUAL THE RANDOM NUMER GENERATED THEN DISPLAY THE QUOTE { echo $row[$column]; break; } }
/*SAMPLE DATABASE SETUP
CREATE DATABASE QUOTES;
USE QUOTES;
CREATE TABLE GET_QUOTES ( REC_QUOTES_ID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, REC_QUOTES TEXT );
INSERT INTO GET_QUOTES VALUES ('','TYPE IN SOME QUOTE');
END SAMPLE DATABASE */ ?>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.