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

How to get this code to MySQL Database?

Discussion in 'Development' started by LewBr, Oct 26, 2017.

  1. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Like i have this on my database:

    tA--------tB
    lewbr - blabla1
    lewbr - blabla2
    lewbr - blabla3
    blabla1 - lewbr
    blabla2 - lewbr
    blabla3 - lewbr

    send message to player who send the command /codetest> blabla1, blabla2, blabla3
    I am trying to do that whit this code:
    PHP:
    if(strtolower($args[0]) == "codetest") {
                             
    $msg null;
                            foreach(
    $this->LISTTEST($player) as $test => $p) {
                                if(
    $test == 'p2') {
                                    
    $msg .= "$p\n ";
                                    return 
    true;
                                }
                                return 
    true;
                            }
                            
    $sender->sendMessage("§cList:\n $msg");
                            return 
    true;
                        }

    public function 
    LISTTEST($tA) {
                    
    $a $this->plugin->provider->db->query("SELECT * FROM list_player WHERE tA='$tA'");
                    
    $b mysqli_fetch_assoc($a);
                    return 
    $b;
            }
    but when i test i just receive the message (List: )
    Why?
     
    Last edited: Oct 26, 2017
  2. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
  3. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    This isn't an SQL forum.
     
  4. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Start debugging! For example you can var_dump() `$b` or `$this->LISTTEST($player)` to check you are getting the expected results.
     
  5. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    What? but i can post an development issue.. ;D
     
  6. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    it isn't but as this already has turned into an how-to-php why not also help with how-to-sql.
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    1. What is $player?
    2. var_dump(LISTTEST($player));
     
  8. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    $player is $player = strtolower($sender->getName());
     
  9. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Now it return just 1 name
    i want to return all names of tA
    example:
    List from LewBr: blabla1, blabla2, blabla3
    i just do /listf and returns:
    List from LewBr: blabla1,
    only...
     
  10. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Your foreach loop contains not one, but two 'return true' statements, so obviously you could only ever get the first item of the array returned by LISTTEST() into $msg, although I can't see how you'd ever have got to sendMessage() unless the result of LISTTEST() is null... but then you haven't posted all the code, as the top half of what you posted doesn't even appear to be in a function. Anyway, delete those two 'return true;' and it should work.
     
    Last edited: Oct 28, 2017
    Muqsit likes this.
  11. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Nope, i have deleted and still get just one player, look how its the database: upload_2017-10-28_18-57-55.png i need to get blabla1 and blabla2 from LewBr and i have removed the returns and set to false, but still getint just one..
     
  12. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That's not what @Awzaw is trying to say. Do you know what a return statement does?
     
  13. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Not so much, sorry. :/
     
  14. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Then remove the return statements in the loop. They not only break the loop, but also the whole function it's being called in aside from returning a value.
    http://php.net/manual/en/function.return.php
     
  15. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Are you sure? It's ALPHA-9 and i think if i remove return on a command event the command doesn't works..
    Look:
    PHP:
    28.10 16:19:35 [ServerINFO string(8"blabla1
    28.10 16:19:35 [Server] INFO "
    Code:
    PHP:
          $msg null;
                            foreach(
    $this->LISTTEST($player) as $friend => $p) {
                                if(
    $friend == 'p2') {
                                    
    $msg .= "$p\n ";
                      
                                }
                          
                            }
                            
    $sender->sendMessage("Test: $msg");
                            
    var_dump($msg);
     
  16. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
  17. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    If that code is in a function which must return a boolean, then you will need a 'return true;', or false depending, after the var_dump(), but at the risk of repeating myself... you've only posted snippets of the code, and this is not a PHP forum.

    Also you should read this http://php.net/manual/en/mysqli-result.fetch-assoc.php, the important part being 'fetched row' in the explanation below:

     
    Last edited: Oct 29, 2017
  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.