July 29, 2010

php script acting weird, shows record even if one does not exist

Question by Krasi

I have a weird problem. Every time i execute this query in php i get the output “Challenge” even if the query is empty (should get “emptyq” if empty) when i test it in phpmyadmin everything is great and query is empty when it should be. I also tried to echo $detectChallengeRes[0][1] and got nothing. I cant find the problem, any help is very appreciated.
The script is suppose to look in the database and check if there is any challenges associated with the current userID, its basically a script that checks if a user has been challenged by another user, the gameID on the current page is the same as the one in the database and that the user hasnt completed the challenge already ($yourscore==0).

$detectChallengeRes = query("SELECT * FROM `AMCMS_challenges` WHERE `gameid`=$gameid AND `winner`=0 AND (`userkey1`=$user OR `userkey2`=$user);");
        if($detectChallengeRes[0][1]!=$user && $detectChallengeRes[0][2]==$user) {
            $yourscore = $detectChallengeRes[0][6]; //Check your score to see if you've already played
        } elseif ($detectChallengeRes[0][2]!=$user && $detectChallengeRes[0][1]==$user) {
            $yourscore = $detectChallengeRes[0][5]; //Check your score to see if you've already played  
        } 
if ($detectChallengeRes!=NULL && $yourscore==0) {
echo 'Challenge';   
} else {
echo 'emptyq';
}

Table structure:

CREATE TABLE IF NOT EXISTS `AMCMS_challenges` (
  `primkey` int(11) NOT NULL auto_increment,
  `userkey1` int(11) NOT NULL,
  `userkey2` int(11) NOT NULL,
  `gameid` int(11) NOT NULL,
  `winner` int(11) NOT NULL,
  `score1` int(11) NOT NULL,
  `score2` int(11) NOT NULL,
  PRIMARY KEY  (`primkey`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

Answer by Starx

This might not solve your question but It looks like it is showing an previous data. Put this before your script

unset($detectChallengeRes);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!