CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 Dude! That totally works. Is it meant to display the first No rows in table with Location id 1 error sometimes as well, too? Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839170 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 What do you mean? Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839188 Share on other sites More sharing options...
CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 Well, the code will correctly display an outcome based on the OutcomeChance, but it will also display the text "No rows in table with Location id 1" sometimes too, as if it was an outcome in the database. Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839195 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 It shouldn't do that unless you're running it through a loop and there is a bad LocationID. Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839199 Share on other sites More sharing options...
CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 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? Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839214 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 Is LocationID a PK? Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839221 Share on other sites More sharing options...
CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 Nope. OutcomeID is the Primary Key. Should LocationID be? Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839222 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 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; Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839228 Share on other sites More sharing options...
CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 lol. The first one is throwing the error - 1. Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839231 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 Do you also get the results from -- echo $row['OutcomeTitle'] . ' | ' . $row['OutcomeImage'] . ' | ' . $row['Outcome']; -- line? Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839236 Share on other sites More sharing options...
CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 Yes. Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839240 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 Weird. IDK. Take out the else statements if you want. Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839255 Share on other sites More sharing options...
CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 Yeah, I hear you. If I take them out, nothing echos. *sadness* lol. I'll mess around with it or something. Thanks Ken2k7 Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839278 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 Post more of the code. Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839281 Share on other sites More sharing options...
CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 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; ?> Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839287 Share on other sites More sharing options...
CloudSex13 Posted May 21, 2009 Author Share Posted May 21, 2009 I'd say the error outcome (now blank according to my code above) is the most common outcome generated. Link to comment https://forums.phpfreaks.com/topic/158965-display-random-row-from-table-on-database/page/2/#findComment-839289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.