I know why I am getting the error Call to a member function query() on a non-object but I am not quite clear on how to fix it.
My connection settings are:
<?php
function db_connect() {
$result = new mysqli('localhost', 'user, 'pass', 'db');
if (!$result) {
throw new Exception('Could not connect to database. Please try again later.');
} else {
return $result;
}
}
?>
In my previous connection settings I had @$db.
My code is
<?php require_once('Connections/db_conn.php');
$query = "SELECT * FROM distance";
$result = $db->query($query);
$num_results = $result->num_rows;
echo"<br />Number of locations: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++) {
//process results
$row = $result->fetch_assoc();
echo"<br />Location: ";
echo stripslashes($row['location']);
echo"Miles: ";
echo ($row['miles']);
}
?>
When I run this code I get the call to a member function () ... error. I know the problem is I no longer have a $db variable in the code. But I also have no idea how to rewrite the code to work with the new connection setting.
In addition, I am not at all sure what these following lines of code mean (well except for the echo🙂:
$result = $db->query($query);
$num_results = $result->num_rows;
echo"<br />Number of locations: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++) {
//process results
$row = $result->fetch_assoc();