I am attempting a connection to a sql db via php and keep getting an error I can't figure out. I can connect with another debug scripts with no errors. I get my connection and pull my data but pulls an error at the end.
$con=mysqli_connect("localhost","username","password","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);
?>
Brings this out
[{"Name":"Apple","Address":"1 Infinity Loop Cupertino, CA","Latitude":"37.331741","Longitude":"-122.030333"},{"Name":"Googleplex","Address":"1600 Amphitheatre Pkwy, Mountain View, CA","Latitude":"37.421999","Longitude":"-122.083954"}]
Warning: mysqli_close() expects parameter 1 to be mysqli, object given in /home/jfletch/public_html/appone/connect.php on line 36
mysqli_close($result);
The line above is incorrect. You only need to call mysqli_close()
once (if at all since, as pointed out in the comments, the connection is closed at the end of the execution of your script) and the parameter should be your link identifier, not your query resource.
Remove it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With