PHP/Mysql query exclude self. Help!

C++, C#, Java, PHP, ect...
Post Reply
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

PHP/Mysql query exclude self. Help!

Post by Rastan »

Hello

I'm having trouble with a piece of code.

What I am trying to do is query the server and have it return me a random player from there that is within a level range of the player logged in. That part works. For those of you who notice- Yep I learned/amlearning off the Tutorials here and I still very much use what I saw there.

What isn't to my liking is that it has the option to return the player name of the player currently logged in ( i.e. you can choose yourself). I'm using this as part of a system that will let a player select an enemy player to do battle with from sort of a quicklist on the index so I'd like for their name not to be selectable even if I wont in the end let the player do combat with itself.

I have tried some different loops and if else type things but I can't get it to work right so I am linking it back at the very basics without my dirty coding attempts .

I'd also like to list @ 3-4 names. At the moment I'm just doing the query 3 times(one time shown)... I'm sure there's a better way to go here, but I'm still sort of bad at this so if you just felt like enlightening me it would be sweet if not I'm more concerned about the other problem.

If someone could please just take a minute and tell me how I would go about stopping it from picking my players name out of the mix from this simple form it would be much appreciated.

Thanks,

Rastan

Code: Select all

<?php
 $playerinfo="SELECT * from player where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);


 $maxlevel = $playerinfo3['level'] + 5;
$minlevel = $playerinfo3['level'] - 5;

if ($minlevel < 1)
{
$minlevel = 1;
}

 $playerdisp ="SELECT * from player WHERE level  < '$maxlevel ' AND level >= '$minlevel' order by rand() limit 1";
$playerdisp2 =mysql_query($playerdisp) or die("could get enemy player!");
$playerdisp3 =mysql_fetch_array($playerdisp2);
$enemychoice1= $playerdisp3['name'] ;

?>


User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP/Mysql query exclude self. Help!

Post by Jackolantern »

This is a very simple fix that requires little to no change to the PHP. Change your query to this:

Code: Select all

$playerdisp ="SELECT * from player WHERE level  < '$maxlevel ' AND level >= '$minlevel' AND name != '$player' order by rand() limit 1";
That will ensure that the players being pulled do not include the player playing the game.

As far as pulling 3 players at a time, you can do this:

Code: Select all

$playerdisp ="SELECT * from player WHERE level  < '$maxlevel ' AND level >= '$minlevel' AND name != '$player' order by rand() limit 3";

//create global array to hold the enemies 
$enemyArray = array();

//process query
if ($result = mysql_query($playerdisp)) {     
     $count = 0;
     //loop through results and store them in enemy array
     while ($row = mysql_fetch_row($result)) {
          $enemyArray[$count] = $row[0]; //see note below. This may need tweaking depending on your db setup.
          $count++;
     }
}  else {
     echo "Could not query database.";
     exit();
}
 
As noted above, "mysql_fetch_row" works a bit differently than fetching an array. The number in $row[0] actually corresponds with which column in the results you are wanting. So $row[0] will get the results of the first column (possibly the table index), $row[1] gets the second column, etc. Count them out to see which one you want, either the enemy name or their player ID, and change the number in "$row[0]" to whichever number corresponds to the row in your table you want. After this code runs, you should have an array of whatever you are going to use to index the enemies. I did not have time to set up all the necessary code to test it, but I copied it out of one of my scripts nearly verbatim, just changing mysqli library to mysql.
The indelible lord of tl;dr
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: PHP/Mysql query exclude self. Help!

Post by Rastan »

Thank you. your first answer did work I haven't tried to incorporate the second into it yet.
I'm confused now tho. I have tried that before and it seemed like it just wanted to ignore the last part so I was thinking maybe I couldn't do another "AND". To clarify it would return me a name just like the code you gave me did but it would still include the $player name as if I had never typed the other "AND" and all with no errors popping up.

Either way thanks for giving me something that works. Maybe one day I'll realize what I had done wrong.

-edit- Case in point I found where one of my code lines still contains what I was doing and still doesn't work. I deleted my line put a 4 after your line and it works so I'm def missing something

I placed your query line on top of mine and spaced it to show if any differences.

Code: Select all

 $playerdisp  ="SELECT * from player WHERE level  < '$maxlevel ' AND level >= '$minlevel' AND name != '$player' order by rand() limit 1";
 $playerdisp4 ="SELECT * from player WHERE level  < '$maxlevel ' AND level >= '$minlevel' AND name != '$player' order by rand() limit 1";
$playerdisp5 =mysql_query($playerdisp) or die("could get a enemy stats!");
$playerdisp6 =mysql_fetch_array($playerdisp5);
  $enemychoice2= $playerdisp6['name'] ;
Last edited by Rastan on Sun Jun 06, 2010 9:20 am, edited 5 times in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP/Mysql query exclude self. Help!

Post by Jackolantern »

Rastan wrote:I'm confused now tho. I have tried that before and it seemed like it just wanted to ignore the last part so I was thinking maybe I couldn't do another "AND". To clarify it would return me a name just like the code you gave me did but it would still include the $player name as if I had never typed the other "AND" and all with no errors popping up.
It was probably just an error in the query. Some complex business queries can have strings of 20+ AND clauses in the same query. Glad it worked, though!
The indelible lord of tl;dr
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: PHP/Mysql query exclude self. Help!

Post by Rastan »

Edited an addition to the post you replied to. I don't know whats going on but if I copy paste what you did it works. I'm not even gonna argue with it. I been messin with that little piece of code for too long . I truly appreciate whatever magic you seem to have worked.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP/Mysql query exclude self. Help!

Post by Jackolantern »

Glad it worked :)
The indelible lord of tl;dr
Post Reply

Return to “Coding”