connection = $mysqli;
$this->connection->set_charset('utf8mb4');
$this->query("SET SESSION sql_mode = 'NO_ZERO_IN_DATE,NO_ENGINE_SUBSTITUTION'");
$this->query("SET FOREIGN_KEY_CHECKS = 0");
// Sync PHP and DB time zones
$this->query("SET `time_zone` = '" . $this->escape(date('P')) . "'");
} catch (\mysqli_sql_exception $e) {
throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname . '!
Message: ' . $e->getMessage());
}
}
/**
* Query
*
* @param string $sql
*
* @return bool|object
*/
public function query(string $sql): bool|object {
try {
$query = $this->connection->query($sql);
if ($query instanceof \mysqli_result) {
$data = [];
while ($row = $query->fetch_assoc()) {
$data[] = $row;
}
$result = new \stdClass();
$result->num_rows = $query->num_rows;
$result->row = isset($data[0]) ? $data[0] : [];
$result->rows = $data;
$query->close();
unset($data);
return $result;
} else {
return true;
}
} catch (\mysqli_sql_exception $e) {
throw new \Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno . '
' . $sql);
}
}
/**
* Escape
*
* @param string value
*
* @return string
*/
public function escape(string $value): string {
return $this->connection->real_escape_string($value);
}
/**
* countAffected
*
* @return int
*/
public function countAffected(): int {
return $this->connection->affected_rows;
}
/**
* getLastId
*
* @return int
*/
public function getLastId(): int {
return $this->connection->insert_id;
}
/**
* isConnected
*
* @return bool
*/
public function isConnected(): bool {
return $this->connection;
}
/**
* Destructor
*
* Closes the DB connection when this object is destroyed.
*
*/
public function __destruct() {
if ($this->connection) {
$this->connection->close();
$this->connection = null;
}
}
}