Fatal error: Call to a member function fetch_assoc() on a non-object
That's because there was an error in your query. MySQli->query()
will return false on error. Change it to something like::
$result = $this->database->query($query);
if (!$result) {
throw new Exception("Database Error [{$this->database->errno}] {$this->database->error}");
}
That should throw an exception if there's an error...
Call to a member function fetch_assoc() on a non-object line 11
Doing it in wrong way -
$query = $mysqli->query("SELECT url FROM urls WHERE url_short = '" . $short . "'");
$result = $query->fetch_assoc();
Fatal error: Call to a member function fetch_assoc() on a non-object in ... on line 17
I think that your query fails to that $conn->query($sql)
will return FALSE
(see mysqli::query).
Please add the following code to your code to get the error message:
if (!$result) {
printf("Error: %s\n", $conn->error);
}
Fatal error: Call to a member function fetch_assoc() on string
$result
variable is used to loop thru mysql query, you have to use different variable names inside your loop
$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "st-datacollector";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM summoner";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<h1>" . $row["SummonerId"]. "</h1>";
$summonerId = $row["SummonerId"];
$url = "https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/" . $summonerId . "/recent?api_key=" . $api_key;
$fcontents = file_get_contents($url);
$resultJSON = json_decode($fcontents);
foreach($resultJSON_decoded->games as $game){
echo $game->gameMode." ".$game->gameType." ".$game->subType;
echo "<br>";
}
}
} else {
echo "0 results";
}
$conn->close();
One more little thing I noticed.. $resultJSON
and $resultJSON_decoded
doesn't match inside the loop
Call to a member function fetch_assoc() on a non-object in. Wrong PHP Code?
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
/* check connection */
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$sql = "SELECT Nr FROM AlleTransaktionen1";
$my_money = 0;
if ($result = $conn->query($sql)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ($row["Nr"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
Fatal error: Call to a member function fetch_assoc() on null on line 9
you can try like this....you have to fetch that array and you can print....
<?php
$mysqli = new mysqli ('localhost', 'root', '', 'db_name');
$mysqli->query("SET NAMES 'utf-8'");
$result_set= mysqli_query($mysqli,"SELECT * FROM products");
$row=mysqli_num_rows($result_set);
echo $row;
while($row=mysqli_fetch_array($result_set))
{
print_r($row);
}
?>
Related Topics
Parse Query String into an Array
File_Get_Contents(): Ssl Operation Failed With Code 1, Failed to Enable Crypto
How to Get Parameters from a Url String
PHP Syntax For Dereferencing Function Result
How to Access This Object Property With an Illegal Name
PHP Function to Generate V4 Uuid
Only Variables Should Be Passed by Reference
How to Display HTML Tags as Plain Text
Should I Manually Check For Errors When Calling "MySQLi_Stmt_Prepare"
Is There a Code Obfuscator For PHP
How to Create Websockets Server in PHP
Create a CSV File For a User in PHP
Automatically Deleting Related Rows in Laravel (Eloquent Orm)