44971

Fatal error: Call to a member function fetch_assoc() on string

I tried to execute a query and iterate thru the result. The echo "<h1>" . $row["SummonerId"]. "</h1>"; works and it print it out at the page. But somehow this Error occurred after the printed result:

Fatal error: Call to a member function fetch_assoc() on string

I saw many errors similar to this. But those are on a non-object and not on string. What is that error?

Here is my code:

$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; $result = file_get_contents($url); $resultJSON = json_decode($result); foreach($resultJSON_decoded->games as $game){ echo $game->gameMode." ".$game->gameType." ".$game->subType; echo "<br>"; } } } else { echo "0 results"; } $conn->close();

Answer1:

$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

Recommend

  • Malformed string while using ast.literal_eval
  • Printing string with %s prints wrong data
  • localhost cannot find stored procedure 'dbo.aspnet_CheckSchemaVersion'
  • PDO throwing error if MYSQL_ATTR_INIT_COMMAND is used
  • Foreach loop using string to output XML
  • PHP code displaying as comments in browser using WAMP [closed]
  • When to use PDO prepared queries. mysql_real_escape error
  • Arrange forum posts by time of latest reply
  • Laravel Eloquent integration in Codeigniter
  • How to run a set of SQL queries from a file, in PHP?
  • Entity Framework unable to delete database, database in use
  • PDO error when wrong host name
  • SetWindowsHookEx does not react on media keys
  • How to revert to previous XCode version?
  • how to avoid repetitive constructor in children
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Spark fat jar to run multiple versions on YARN
  • output of program is not same as passed argument
  • Where to put my custom functions in Wordpress?
  • ActionScript 2 vs ActionScript 3 performance
  • Akka Routing: Reply's send to router ends up as dead letters
  • Numpy divide by zero. Why?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Trying to get generic when generic is not available
  • How do I configure my settings file to work with unit tests?
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs
  • Why do underscore prefixed variables exist?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?