Forum Moderators: coopster
$rand = [url=http://us2.php.net/manual/en/function.rand.php]rand[/url](0,$num_of_rows); //you can query the database to get num of rows if you'd like
$query = "select * from table_name";
$result = [url=http://us2.php.net/manual/en/function.mysql-query.php]mysql_query[/url]($query) or [url=http://us2.php.net/manual/en/function.die.php]die[/url]([url=http://us2.php.net/manual/en/function.mysql-error.php]mysql_error[/url]());
$info = [url=http://us2.php.net/manual/en/function.mysql-result.php]mysql_result[/url]($result,$rand);
echo $info;
Good luck!
Another way I was thinking, instead of having all the possibilities listed in a MySQL database and randomly plucking 1, was listing all the possibilities in the actual code and plucking 1. Like this:
<?php
switch(rand(1,9)) {
case 1:
$fork = 'bang'; break;
case 2:
$fork = 'dsfdg'; break;
case 3:
$fork = 'dffdg'; break;
case 4:
$fork = 'fdgg'; break;
case 5:
$fork = 'fgdfsg'; break;
case 6:
$fork = 'fdgg'; break;
case 7:
$fork = 'fdgg'; break;
case 8:
$fork = 'ggdgg'; break;
case 9:
$fork = 'gffdsg'; break;
}
print $fork;
?>
I have thousands of possibilities so the code will run to thousands of lines. I'm just writing here for clarification that the MySQL method should run faster than the method outlined here. I think I know the answer - but would like someone more experienced to confirm. if anyone would be so kind.
<?php
$dbh=mysql_connect ("localhost", "china_for", "password") or die
('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("china_random");
$rs = mysql_query("SELECT frampton FROM mikey ORDER BY RAND() LIMIT 1");
$row = mysql_fetch_row($rs);
return $row['field1'];
?>
Make sure the field name you're using in $row[''] is a fieldname from your query. You can change return to echo in the last line to have it output the value.
<?php
$dbh=mysql_connect ("localhost", "china_for", "password") or die
('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("china_random");
$rs = mysql_query("SELECT frampton FROM mikey ORDER BY RAND() LIMIT 1");
$row = mysql_fetch_row($rs);
echo $row['frampton'];
?>
you could try adding an or die to your query to see if that is dying
$rs = mysql_query("SELECT frampton FROM mikey ORDER BY RAND() LIMIT 1") or die('<p>query died: ' . mysql_error());
also are there any of the rows where the 'frampton' column might be empty?
<added>the other thing is it looks like mysql_fetch_row returns a numbered array so you might want to try
echo $row[0];
<?php
$dbh=mysql_connect ("localhost", "china_for", "password") or die
('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("china_random");
$rs = mysql_query("SELECT frampton FROM mikey ORDER BY RAND() LIMIT 1");
$row = mysql_fetch_row($rs);
echo $row[0];
?>
I am unsure what the last 2 posts meant. WIll they offer any improvement on this working code?