I'm writing a tire overview website as a project for internet programming. I currently have a database connection file, a header and a php file to show the data from the database.
The database Connection Class is included in the header. The header is included in my controller php class and the controller class is included in the View Class.
Now my Problem: I can use my Database Variable inside my Controller File, as long as it is not inside of a function. If I try to write a function inside the Controller File, it won't find the Database Variable.
I tried making the $db variable global and tried many different variants of connecting to the database. The connection is always working, but I can't access the variable inside of a function. Please help
Controller File
include '../../header.php';
// Bisherige Manufacturer Auslesen
$sql = 'SELECT * FROM tire_manufacturer';
$result = $db->rawQuery($sql, Array(10));
$rows = array();
foreach ($result as $row){
$row['count'] = getModelCountByManuid($row['id']);
array_push($rows, $row);
}
function getModelCountByManuid($manu_id){
$sql = 'SELECT COUNT(id) as counti FROM tire_model WHERE cat_id_manufacturer = ' . $manu_id;
$result = $db->rawQuery($sql, Array(10));
}
Header File
include "Helper/MyCrypt.php";
require_once ('Helper/MysqliDb.php');
$db = new MysqliDb ('rdbms.strato.de', '****', '****', '****');
?>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="../CSS/carspace.css" type="text/css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">```