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:
<?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->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);
?>
I run my code and heres what I get :
Thank you guys.
EDITED:
I changed to mysqli, now it looks like this:
<?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->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);
?>
still getting exact same error