im creating a small webservice and i need set some restfull api.
The problem is that i set the connection with db with a method inside Database class like this:
public function connect() {
echo "Starting connection\n";
// Create connection
$conn = new mysqli($this->db_name, $this->username, $this->password);
// Check connection
if (!$conn) {
die("Database connection failed: " . $conn->mysqli_connect_error());
}
echo "Connected successfully";
return $conn;
}
On my api method for city model i do this:
$database = new Database();
$db = $database->connect();
$result = $city->get();
// If db get connected
if($db) {
$city = new City($db);
$result = $city->get();
// If query go well
if($result) {
// lot of code is done here
else {
echo connection failed with DB";
}
On broswer i get this messages:
Creating DB handler (Constructor of Database object)
Starting connection (Connect method called in API)
Connected successfully (Connection success)
Query not executable (in City models query failed for some reason do not know why)
No connection made with DB! (Connection now fail when in API i check it again before shown data)
I didn't post all file, if it's needed i can do it