title: encoding data from MySql to JSON
body: <p>I have mysql database online on my domain, and I have a simple table called "Locations" with some data in it, I want to call in the data in an array, then encode the array with json and finally echo out the encoded json data. Here's my code, my problem in is putting data in array and encoding it to jso:</p>
  
  <pre><code>&lt;?php
  // obtaining connection : successful!
  $con = mysql_connect("myhost", "username", "password");
  $con or die(mysql_error());
  mysql_select_db("database_name") or die(mysql_error());
  
  // selecting all from table "locations" : successful!
  $result = mysql_query("SELECT * FROM Locations") 
  or die(mysql_error()); 
  
  // puting data into array and encode it with json : FAILED! 
  $resultArray = array();
  $tempArray = array();
  
  // Loop through each row in the result set
  while($row = $result-&gt;fetch_object())
  {
      // Add each row into our results array
      $tempArray = $row;
      array_push($resultArray, $tempArray);
  }
  
  // Finally, encode the array to JSON and output the results
  echo json_encode($resultArray);
  
  // closing connection : successful!
  mysql_close($con);
  ?&gt;
  </code></pre>
  
  <p>I run my code and heres what I get : </p>
  
  <p><img src="https://i.stack.imgur.com/hmiXJ.png" alt="This is what I get when launching code above"></p>
  
  <p>Thank you guys.</p>
  
  <p>EDITED:</p>
  
  <p>I changed to mysqli, now it looks like this: </p>
  
  <pre><code>&lt;?php
  
  // Create connection
  $con=mysqli_connect("host","user","pass","dbname");
  
  // Check connection
  if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  // This SQL statement selects ALL from the table 'Locations'
  $sql = "SELECT * FROM Locations";
  
  // Check if there are results
  if ($result = mysqli_query($con, $sql))
  {
  // If so, then create a results array and a temporary one
  // to hold the data
  $resultArray = array();
  $tempArray = array();
  
  // Loop through each row in the result set
  while($row = $result-&gt;fetch_object())
  {
      // Add each row into our results array
      $tempArray = $row;
      array_push($resultArray, $tempArray);
  }
  
  // Finally, encode the array to JSON and output the results
  echo json_encode($resultArray);
  }
  
  // Close connections
  mysqli_close($result);
  mysqli_close($con);
  ?&gt;
  </code></pre>
  
  <p>still getting exact same error</p>