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

Mysql Help

Discussion in 'Off-Topic' started by Junkdude, May 20, 2017.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    So I know this isnt a PHP forums hence why i poste it in off topic, but my function is getting a notice. "Trying to get property of non-object". Heres the function thats having the issue.

    PHP:
    public function existsElo($player){
            
    //$p = trim(strtolower($player->getName()));
            
    $result $this->elo->query("SELECT * FROM playerdata WHERE 'name' = " $player ";");
            return 
    $result->num_rows true:false;
        }
     
  2. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    By the way I replicated economyapi's way of doing it.
     
  3. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    So
    PHP:
    $count count($result);
            return 
    $count->num_rows true:false;
     
  4. Michael

    Michael Baby Zombie

    Messages:
    113
    GitHub:
    michaelm04
    I'm assuming your actually querying from a MySql db as stated, try this, its one of many ways to see if data is returned:
    PHP:
    public function existsElo($player){
        
    $result $this->elo->query("SELECT * FROM playerdata WHERE name = ' " $player " ';");
        
    $data $result->fetch_assoc();
        if(!
    is_array($data)){
            return 
    false;
        } else {
            return 
    true;
        }
    }
    Remove the spaces in between " & ' just there to show you how it works. Also, might I recommend just fetching one value? Example: "SELECT name", since it's just to see if the player exists in the database don't fetch extra data you don't need.
     
    Last edited: May 21, 2017
  5. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Ignore me, and follow @Michael's advice here - you do indeed need to get the array from the $result using fetchArray() or fetchArray(SQLITE3_ASSOC) if it's SQLITE.
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    There is nothing wrong with the PHP code. The problem is that you don't understand how mysqli queries work.
    When you execute a query, you pass the string to the database. In your code:
    I don't know what your $player is. Most likely a player name like "SOFe". So the query becomes:
    Code:
    SELECT * FROM playerdata WHERE 'name' = SOFe
    
    There are two problems in the query:
    1. 'name' is a literal string. I don't think you want to resolve a literal string here -- instead, you want to reference a column name. You should use backticks to quote the column name, or simply don't quote it (because name is not a forbidden keyword at this point):
    Code:
    SELECT * FROM playerdata WHERE `name` = ?
    -- or, without quoting:
    SELECT * FROM playerdata WHERE name = ?
    
    where ? is your player name
    2. You are not quoting the player name. SOFe becomes a database column to resolve, which doesn't exist. You don't want to reference a column here; instead, you want this to be a literal string. So, you should quote it:
    PHP:
    $elo->query("SELECT * FROM playerdata WHERE name = '$player'");
    However, this is vulnerable to SQL injection. There are too many articles about SQL out there, such as bobby-tables.com. Read them yourself.
    The mysqli functions for binding parameters are very verbose though. Consider using libasynql instead. (You should do the queries asynchronously anyway, as recommended in PQRS 1.2.
    PHP:
    3. The trailing semicolon is not neededmysqli ignores any delimiters.
    4. You are querying excessive dataYou simply want to know whether there are rows matching name $playerYou could use COUNT(*) for this:
    [
    php]
    $stmt $this->elo->prepare("SELECT COUNT(*) AS cnt FROM playerdata WHERE name = ?");
    $stmt->bind_param("s"$player);
    $result $stmt->execute();
    $stmt->close();
    $cnt = (int) $result->fetch_assoc()["cnt"]; // there should be one row all the time
    return $cnt 0;
    We rarely check whether a row exists in a plugin, though. If you want the player data, download them all directly, or return a default set of values if no data could be downloaded. Don't waste time (could take several milliseconds!) checking if the row exists before downloading it. If you need to decide whether to INSERT or UPDATE, consider using the "INSERT INTO ... ON DUPLICATE KEY UPDATE ..." syntax.
     
  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.