Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

Php Programming Code Examples

Php > Database Related Code Examples

Random Quote - ( Functional )

Random Quote - ( Functional ) <?php $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 */ ?>