Jump to content

Display Random Row From Table On Database


CloudSex13

Recommended Posts

I haven't adjusted the code you made, but I do have outcomes in the database where the LocationID is different numbers (for different locations) from the CurrentLocation field. Is their a way for these to not show? I would have guessed the WHERE LocationID='$currentLocation' in the SQL queries would have worked for this, no?

No, if LocationID is a PK, then it would be stupid.

 

Let's see which one is throwing the error message -

$currentLocation = intval($currentLocation);
$query = mysql_query('SELECT SUM(OutcomeChance) FROM Outcomes WHERE LocationID = ' . $currentLocation) or trigger_error(mysql_error());
if ($sum = mysql_fetch_row($query)) {
     $rand = rand(1, intval($sum[0]));
     $query2 = mysql_query('SELECT * FROM Outcomes WHERE OutcomeChance >= ' . $rand . ' AND LocationID = ' . $currentLocation . ' ORDER BY RAND() LIMIT 1;') or trigger_error(mysql_error());
     if ($row = mysql_fetch_assoc($query2)) echo $row['OutcomeTitle'] . ' | ' . $row['OutcomeImage'] . ' | ' . $row['Outcome'];
     else echo '1. No rows in table with Location id ' . $currentLocation;
} else echo '2. No rows in table with Location id ' . $currentLocation;

outcome.php:

 

<?php

include('config.php');
include('global.php'); // Contains $currentLocation
include('checklogin.php');

if ($currentLocation == "1") {
$query = mysql_query('SELECT SUM(OutcomeChance) FROM Outcomes WHERE LocationID = ' . $currentLocation) or trigger_error(mysql_error());
if ($sum = mysql_fetch_row($query)) {
     $rand = rand(1, intval($sum[0]));
     $query2 = mysql_query('SELECT * FROM Outcomes WHERE OutcomeChance >= ' . $rand . ' AND LocationID = ' . $currentLocation . ' ORDER BY RAND() LIMIT 1;') or trigger_error(mysql_error());
     if ($row = mysql_fetch_assoc($query2)) { 
$outcomeTitle = $row['OutcomeTitle']; 
$outcome = $row['Outcome'];
     }
} else echo 'No rows in table with Location id ' . $currentLocation;

$results = "
<p><strong>".$outcomeTitle."</strong></p>
".$outcome."
<p><a href=\"outcome.php\">Get A Outcome...</a></p>";

} else {

header("Location: home.php");
die();

}

echo $results;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.