codexx Posted June 23, 2007 Share Posted June 23, 2007 Hey Everyone, I have to do a script where a user fights an enemy. There are many to choose from all for differn't levels in which the user is at. I need to first (SELECT * FROM creep_data WHERE minlvl < '".$user[lvl]."' AND maxlvl > '".$user['lvl']."'") After I have all of the possible monsters I need to randomly select one of these and retrieve all of the data about it. How could I go about doing this ? Thanks in advance, Sean Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/ Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 use: order by id rand() in ur query. Here id primary key. any thing it can depends on ur table structure. Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/#findComment-280715 Share on other sites More sharing options...
codexx Posted June 23, 2007 Author Share Posted June 23, 2007 Primary key is ID. When I try $creepinfo = mysql_query ("SELECT * FROM creep_data WHERE minuserlvl < '".$user[lvl]."' AND maxuserlvl > '".$user['lvl']."' ORDER BY id rand()"); it gives me an error. Is there anyway I could get all off the possible creeps then randomly select on in an array? Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/#findComment-280716 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 What is that error. Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/#findComment-280718 Share on other sites More sharing options...
codexx Posted June 23, 2007 Author Share Posted June 23, 2007 Error reporting is turned off so i'm not sure the exact error. Its just saying that its an invalid SQL statement when I check it. Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/#findComment-280719 Share on other sites More sharing options...
hyeteck Posted June 23, 2007 Share Posted June 23, 2007 im still trying to get a hang of this php stuff myself, but try this, it should work. $creepinfo = mysql_query ("SELECT * FROM creep_data WHERE minuserlvl < '$user[\'lvl\']' AND maxuserlvl > '$user[\'lvl\']' ORDER BY id rand()"); Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/#findComment-280720 Share on other sites More sharing options...
php_joe Posted June 23, 2007 Share Posted June 23, 2007 or I think you could use shuffle($creepinfo); and then use $creepinfo[0] for the enemy's information. Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/#findComment-280770 Share on other sites More sharing options...
sasa Posted June 23, 2007 Share Posted June 23, 2007 SELECT * FROM creep_data WHERE minuserlvl < '$user[\'lvl\']' AND maxuserlvl > '$user[\'lvl\']' ORDER BY rand() LIMIT 1 no id field in order Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/#findComment-280812 Share on other sites More sharing options...
codexx Posted July 10, 2007 Author Share Posted July 10, 2007 The last post worked Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/56816-solved-randomly-select-a-row/#findComment-294132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.