Tutorials

Random quote

This is a code you can use to show a random quote at any page you wish.

Open up notepad or any other text/HTML editor and create a new file. Paste this into it:

<?php
require "config.php";

$result = mysql_query("SELECT * FROM $table ORDER BY RAND() LIMIT 1");
$row = mysql_fetch_assoc($result))

echo "<em>" . $row["quote"] . "</em> --- " . $row["author"];
?>

Save it as random.php and upload it to the same folder as your quotes scripts. Now you can just include it to whatever page you want a random quote to show up!

Important Notes:

  1. If it gives you an error, try editing the second line, where the config.php file is included, and make it include the full path, something like:

    require "/home/user/public_html/phpquotes/config.php";
  2. If you are using a new version of MySQL (5.0+), you might want to try RAND(NOW()) instead of RAND(). So your third line would look like this:

    $result = mysql_query("SELECT * FROM $table ORDER BY RAND(NOW()) LIMIT 1");

Comments

Error Comments are closed for this entry.