1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved How to get only the TOP #1 the best player on Database?

Discussion in 'Help' started by LewBr, Jul 17, 2017.

  1. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    I'm trying to get only the Top1 Player in my Database and i use this code that doesn't works!
    EVENT:

    PHP:
          public function getRankingTest() {
            
    $records = [ ];
            try {
                
    $prepare $this->provider->database->prepare "SELECT * FROM `pvp_stats` ORDER BY `kills` DESC LIMIT 3");
                
    $result $prepare->execute ();
                if (
    $result instanceof \SQLite3Result) {
                    while ( 
    $data $result->fetchArray SQLITE3_ASSOC ) ) {
                        
    $records [] = $data;
                    }
                    
    $result->finalize ();
                }
            } catch ( \
    Exception $e ) {
                
    $this->plugin->printError($e) ;
            }
            return 
    $records;
        }
    COMMAND:
    PHP:
                case "teste1":
     
                
    $ranking $this->getRankingTest();
               if (
    count $ranking ) == 1) {
                
    $top1 $ranking [0] ["name"];
                
    $sender->sendMessage("top 1 best player: ".$top1."");
                }
               break;
    when i try to use the command, this is the error:

    17.07 13:20:10 [Server] Server thread/INFO An unknown error occurred while attempting to perform this command 17.07 13:20:10 [Server] Server thread/CRITICAL Unhandled exception executing command 'teste1' in teste1: Cannot use object of type mysqli_result as array 17.07 13:20:10 [Server] Server thread/CRITICAL Error: "Cannot use object of type mysqli_result as array" (EXCEPTION) in "Main" at line 122

    Line 122:
    PHP:
     if (count $ranking ) == 1) { 


    i don't know how to fix, i'm bad at DATABASE can anyone help me, and make an example? Thk
    Sorry for my english..
     
    Last edited: Jul 17, 2017
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Thanks for the good explaining, but there's one problem:
    I can't read your code, it's pink on grey background (in the dark theme), close to unreadable, please consider using code blocks ([php]Put your code here[/php]) for posting code
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Apparently you're mixing up MySQL with SQLite. It gives an error about MySQL while you're checking for an SQLite3 result.
     
  4. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Now, much better?
     
  5. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Oh, ok but how can i do this whit MySQL? I don't know..
    Just getting the 1° best player..
     
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Yes, Thank you very much :)
     
    LewBr likes this.
  7. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Try just removing the instanceof SQLite3QueryResult line.
     
  8. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Like so:
    PHP:
            public function getRankingTest() {
            
    $records = [ ];
            try {
                
    $prepare $this->provider->database->prepare "SELECT * FROM `pvp_stats` ORDER BY `kills` DESC LIMIT 3");
                
    $result $prepare->execute ();
            } catch ( \
    Exception $e ) {
                
    $this->plugin->printError($e) ;
            }
            return 
    $records;
        }
    ?
     
  9. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    When i do this, i try to run the command "/teste1" and doesn't do anything, and doesn't have any error on Console :/
     
  10. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    just need the code to only get the name of the first place in the ranking whit my Database..
     
  11. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    I have fixed, sorry, thanks to all!
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.