Free as Freedom, not Free as Free Pizza!” />

-

Udruženje Informatičara BiH - Cross-platform and Portable Development With PHP, LAMP/WAMP development, AJAX and Javascript, ASP, dot NET, C sharp, C++, C, VB, Oracle, tutorials and tips...
Free as Freedom, not Free as Free Pizza!


 
Web http://www.uibih.co.nr

Add to Google

Tuesday, September 13, 2005

Take a random row from the database..

This code is written by PHP, it will access MySQL database and take a random row then put it in an array.
#######################
function connect_easy($host,$db,$user,$pass,$query)
{
if(!$connect = @mysql_connect($host,$user,$pass))
echo mysql_error();
if(!$db = @mysql_select_db($db))
echo mysql_error();
if(!($result = @mysql_query($query)))
echo mysql_error();
@$num = mysql_num_rows($result);
@$num2 = mysql_num_fields($result);
for($x=0;$x<$num;$x++)
{
$a = mysql_fetch_array($result);
for($i=0;$i<$num2;$i++)
{
$b[$x][$i] = $a[$i];
}
}
return $b;
}
// begin variables
$host="the host name of your server";
$db="your database name";
$table="The table name you want to retrieve the data from";
$user="the username";
$pass="the password";
$query="select * from ".$table;
// end variables
// Proccess
$rows=connect_easy($host,$db,$user,$pass,$query);
$rand=rand(0, count($raw));
$the_random_raw=$rows[$rand];
// now you have a random row with the needed data, just pick up the data you need
########################