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 > 0 ? true:false; }
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.
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.
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 needed. mysqli ignores any delimiters.4. You are querying excessive data. You simply want to know whether there are rows matching name = $player. You 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 timereturn $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.