- Forums
- MySQL
- How To Display Random Information From MySQL Database PHP Function
This Page Contains information about How To Display Random Information From MySQL Database PHP Function By wallpaperama in category MySQL with 0 Replies. [858], Last Updated: Sat May 18, 2024
wallpaperama
Tue Sep 05, 2006
0 Comments
1137 Visits
Sometimes you want display random information from your database. I had a hardtime figuring out how to do is with php. I found the RAND() functions works greate with MySQL. Here I have create a snippet to help give you a sample code script:
The following example will display one output from your table. You can display multiple fields by doing a loop.
example: Make sure to provide your database information like your username, password, etc..
CODE:
<?
# Random Snippet Code Sample by http://www.wallpaperama.com
# 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];
?>
* make sure to edit the script and provide your correct
table_name and
field_name from your table, ( i used table_name and field_name as an example)