Fatal Error: Call to a Member Function Fetch_Assoc() on a Non-Object

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



Leave a reply



Submit