PHP Snippets
Name:MySQL Random Rows
Description: This will display a random row from specific MySQL database and table.
Code below...
>
<?php
//Change the information below
$dbhost = "localhost";
$dbname = "database_name";
$dbuser = "database_user";
$dbpass = "database_user_pass";
$dbtable = "table_here";
//Connect and select database
$db = mysql_connect($dbhost,$dbuser,$dbpass) ;
mysql_select_db($dbname);
//This will display random rows and put it in an array
$result = mysql_query("SELECT * FROM $dbtable ORDER BY RAND()");
$r = mysql_fetch_array($result);
//Change this to the field you want to display randomly from the table
$item = "someitem";
//Display the result
echo $r["$item"];
mysql_close($db);
?>
//Change the information below
$dbhost = "localhost";
$dbname = "database_name";
$dbuser = "database_user";
$dbpass = "database_user_pass";
$dbtable = "table_here";
//Connect and select database
$db = mysql_connect($dbhost,$dbuser,$dbpass) ;
mysql_select_db($dbname);
//This will display random rows and put it in an array
$result = mysql_query("SELECT * FROM $dbtable ORDER BY RAND()");
$r = mysql_fetch_array($result);
//Change this to the field you want to display randomly from the table
$item = "someitem";
//Display the result
echo $r["$item"];
mysql_close($db);
?>