How To Generate Random Tables Fields From MySQL PHP Function Snippet

RegisterLogin
How To Generate Random Tables Fields From MySQL PHP Function Snippet
Post Description: How To Generate Random Tables Fields From MySQL PHP Function Snippet
Tags: How, To, Generate, Random, Tables, Fields, From, MySQL, PHP, Function, Snippet
This Post Was Posted On Apr 25, 2008 By misc #1498
if you want to generate random information from your mysql tables, all you have to do is use the RAND() function:

The following example will display one output from your table. You can display multiple fields by doing a loop.

example:

<?
# CONFIGURE
$db_host = 'localhost';
$db_username = 'myusername';
$db_password = 'mypassword';
$db_dbname = 'mydatabase_name';
# END CONFIGURE

$database = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_dbname,$db);

$sql = "SELECT * FROM table_name ORDER BY RAND()";
$result = mysql_query($sql ,$db);
$myrow = mysql_fetch_array($result);

echo $myrow[field_name];

?>
Leave Your Comments
Related Pages: [Add Your Website]
Post New Topic
voimmeli
#2783 1
i always used rand() to get a random value from my table using mysql to generate random queries. it works great. but sometimes i noticed it does slow down when my website has more than 10 people online so i started to use a php function instead, if you are interested in the function, let me know i can send it to you
Nov 10, 2009 Reply Report abuse
alex
#2593 2
does this work on how to generate random from my sql with php
Oct 15, 2009 Reply Report abuse
©2011 Webune Forums - Tue Dec 20, 2011 4:24 pm
Powered by: Webune Forums V3