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

Variable won't update

Discussion in 'Development' started by FadedMarksman, Oct 6, 2017.

  1. FadedMarksman

    FadedMarksman Creeper

    Messages:
    4
    Hi, I can't seem to get my variable to update when I call the function that updates it. MySQL runs and sends all the data, but it still won't update the variable.

    Code that updates the variable.
    PHP:
    public $group "";
        
    /** @var \mysqli */
        
    protected $sql;

        public function 
    __construct(Player $playerCore $core){
            
    $this->sql $core->sql;
        }

        public function 
    sendJoinData(Player $player){
            
    $group "guest";
            
    $user $player->getName();
            
    $prepare $this->sql->prepare("INSERT IGNORE INTO players(username, groupUser) VALUES(?, ?)");
            
    $prepare->bind_param("ss"$user$group);
            
    $prepare->execute();
            
    $prepare->close();
            
    $this->getData($player);
        }

        public function 
    getData(Player $player){
            
    $result $this->sql->query("SELECT * FROM players WHERE username='{$player->getName()}'");
            
    $data = [];
            while(
    $row $result->fetch_assoc()){
                
    $data $row;
            }
            
    $this->group $data["groupUser"];
        }

    Misc:
    PHP:
    public function onLogin(PlayerLoginEvent $ev){
            
    $player $ev->getPlayer();
            
    $this->getData($player$this->sql)->sendJoinData($player);
        }
     
    Last edited: Oct 6, 2017
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    You have not provided enough of your code to allow us to aid you in resolving your issue
     
    sharletsings123 likes this.
  3. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    At a glance the first obvious error is that in onLogin the call to getData doesn't return anything - which is unexpected considering the name - let alone an object which has the method sendJoinData.
     
    Last edited: Oct 6, 2017
  4. FadedMarksman

    FadedMarksman Creeper

    Messages:
    4
    That's all the code to it...
     
  5. FadedMarksman

    FadedMarksman Creeper

    Messages:
    4
    So should I return the variables on the getData function?
     
  6. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    It sounds as though you need to learn some more PHP if you are going to develop a plugin - is the code yours, or are you trying to fix code written by someone else?
     
    jasonwynn10 and iiFlamiinBlaze like this.
  7. FadedMarksman

    FadedMarksman Creeper

    Messages:
    4
    It's my code.
     
  8. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    OK, then what this does:
    Code:
    $this->getData($player, $this->sql)->sendJoinData($player);
    Is look in $this (the class to which current code belongs) for the function getData(), to which it sends the arguments $player and $this->sql. Then it looks for the method (function) sendJoinData on the object returned by getData... but since you don't return anything from getData(), that function will not be found. Either you need to return $this from getData()... or simply break the line into two, one to run getData, the next to run sendJoinData(). Again, I haven't looked closely so there may well be other problems too, but this one shows that you aren't really familiar with how objects work.
     
    jasonwynn10 likes this.
  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.