Database Code

  Home arrow Database Code arrow Random Quote - ( Functional )
DATABASE CODE

Random Quote - ( Functional )
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-08-15

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap