Results 1 to 9 of 9

Thread: Newbie question

  1. #1
    Join Date
    May 2013
    Posts
    6

    Unanswered: Newbie question

    Hi all , I have searched and tryed to fix my problem but without luck , it whould be nice if someone can give me a hand .

    Here is my function for interact with mysql :

    Code:
    function IncreasePlayerScore( $sid, $player ) {
        // Update information about the player in database
        $tbl = SERVER_TABLE_NAME.$sid;
        $kia = -intval($player['kia']) / 4;
        $roe = -intval($player['roe']);
        $cmd = "INSERT INTO `$tbl` (`name`, `score`, `goal`, `leader`, `enemy`, `kia`, `roe`) VALUES('"
            .mysql_real_escape_string($player['name'])."', '".$player['score']."', '".$player['goal']."', '".$player['leader']."', '".$player['enemy']."', '$kia', '$roe'"
            .") ON DUPLICATE KEY UPDATE `score`=score+'".$player['score']."', `goal`=goal+'".$player['goal']."', `leader`=leader+'".$player['leader']
            ."', `enemy`=enemy+'".$player['enemy']."', `kia`=kia+'$kia', `roe`=roe+'$roe'"
            .";";
        if( !mysql_query($cmd) ) echo "Error updating player info: ".mysql_error()."<br />$cmd";
    }
    this functions does update the players score , it does adds the new score to the old one. What I need to change is on this it should only replace if the new score is higher then the old. And not calculate together just update new score if higher.

    My second question is how can I run this function in php ?



    Thank you

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Modify your ON DUPLICATE KEY clause to behave as you'd like (replace instead of add, and only if the new score is higher).

    I would expect that you could call this function like any other. What problems are you having (what doesn't work for you) ?

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    May 2013
    Posts
    6
    Quote Originally Posted by Pat Phelan View Post
    Modify your ON DUPLICATE KEY clause to behave as you'd like (replace instead of add, and only if the new score is higher).

    I would expect that you could call this function like any other. What problems are you having (what doesn't work for you) ?

    -PatP

    ON DUBLICATE KEY what is it exactly to change , I really dont no mysql .

    For my second question aqtually I have not did try this to save lilke a php page and run it I will try this . thanks for your answers.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    This is pure "air code", but my first guess would be:
    PHP Code:
        $cmd "INSERT INTO `$tbl` (`name`, `score`, `goal`, `leader`, `enemy`, `kia`, `roe`) VALUES('"
            
    .mysql_real_escape_string($player['name'])."', '".$player['score']."', '".$player['goal']."', '".$player['leader']."', '".$player['enemy']."', '$kia', '$roe'"
            
    .") ON DUPLICATE KEY UPDATE `score`= '".$player['score']."', `goal`= '".$player['goal']."', `leader`= '".$player['leader']
            .
    "', `enemy`= '".$player['enemy']."', `kia`= '$kia', `roe`= '$roe'"
            
    ." WHERE score < ".$player['score']";"
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  5. #5
    Join Date
    May 2013
    Posts
    6
    Quote Originally Posted by Pat Phelan View Post
    This is pure "air code", but my first guess would be:
    PHP Code:
        $cmd "INSERT INTO `$tbl` (`name`, `score`, `goal`, `leader`, `enemy`, `kia`, `roe`) VALUES('"
            
    .mysql_real_escape_string($player['name'])."', '".$player['score']."', '".$player['goal']."', '".$player['leader']."', '".$player['enemy']."', '$kia', '$roe'"
            
    .") ON DUPLICATE KEY UPDATE `score`= '".$player['score']."', `goal`= '".$player['goal']."', `leader`= '".$player['leader']
            .
    "', `enemy`= '".$player['enemy']."', `kia`= '$kia', `roe`= '$roe'"
            
    ." WHERE score < ".$player['score']";"
    -PatP
    no this didnt work all the tables get "Nann" or " undefinied value " result

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Hmmm... So every row in every table in your database is corrupted after you run this statement?

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  7. #7
    Join Date
    May 2013
    Posts
    6
    yes exactly it should show for example 12 different sc ore tables but it shows only 2 and they have no entiries only nann

  8. #8
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I don't have any clue then. You may have a corrupt MySQL install, a serious flaw in your PHP, or some other problem that I haven't considered.

    This is certainly beyond my ability to help you over the internet. You need someone with more insight or better yet someone that can sit beside you and work through the issues.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  9. #9
    Join Date
    May 2013
    Posts
    6
    hmm well , I get your code working like this :

    Code:
     $cmd = "INSERT INTO `$tbl` (`name`, `score`, `goal`, `leader`, `enemy`, `kia`, `roe`) VALUES('"
            .mysql_real_escape_string($player['name'])."', '".$player['score']."', '".$player['goal']."', '".$player['leader']."', '".$player['enemy']."', '$kia', '$roe'"
            .") ON DUPLICATE KEY UPDATE `score`='".$player['score']."', `goal`=goal+'".$player['goal']."', `leader`=leader+'".$player['leader']
            ."', `enemy`=enemy+'".$player['enemy']."', `kia`=kia+'$kia', `roe`=roe+'$roe'"
    	." WHERE score < ".$player['score']
            .";";
    like this the page opens without any errors but it doesnt add the new score ? I took the space after score= ( deleted)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •