I wrote a piece of code. and to be honest it seems a bit messy so I would like some opinions on it, and if I can make it cleaner. Or if there are better ways to do it:
<?php
include "Databaseconnect.php";
$result = $database->prepare("SELECT * FROM user WHERE Username=?");
$result->bindParam(1, $_SESSION['username']);
$result->execute();
$fetchedData = $result->fetch(PDO::FETCH_ASSOC);
$result = $database->prepare("SELECT * FROM garage WHERE Garage_Naam=?");
$result->bindParam(1, $fetchedData['Company']);
$result->execute();
$fetchedDataCompany = $result->fetch(PDO::FETCH_ASSOC);
$result = $database->prepare("SELECT * FROM factuur WHERE Garage_Nummer=?");
$result->bindParam(1, $fetchedDataCompany['Garage_Nummer']);
$result->execute();
$countRowsExistence = $result->rowCount();
if($countRowsExistence != 0) {
$fetchedDataInvoice = $result->fetch(PDO::FETCH_ASSOC);
$result = $database->prepare("SELECT * FROM factuur_regel WHERE
Factuur_Nummer=?");
$result->bindParam(1, $fetchedDataInvoice['Factuur_Nummer']);
$result->execute();
$fetchedDataInvoiceRegel = $result->fetchAll(PDO::FETCH_ASSOC);
foreach($fetchedDataInvoiceRegel as $key => $item) {
$stmt = $database->prepare("SELECT * FROM product WHERE
Product_Nummer=?");
$stmt->bindParam(1, $item['Product_Nummer']);
$stmt->execute();
$fetchedDataProduct[$key] = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
} else {
header("location: landingpage.php");
}