I use a php code to filter some data as a search engine in a sql database and send the results as json to the index where there is a function that will load these results, in localhost with xampp it works well and fast but on my online host I keep getting this error: Uncaught (in promise) SyntaxError: Unexpected end of JSON input
at index.php:866:29
there is the php code:
<?php
include 'config.php';
// Connexion à la base de données
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_error) {
die("Erreur de connexion à la base de données: " . $mysqli->connect_error);
}
// Récupération du terme de recherche et formatage
$searchTerm = isset($_GET['recherche']) ? $_GET['recherche'] : '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$projetsParPage = 2; // Nombre de projets affichés par page
$offset = ($page - 1) * $projetsParPage;
$searchTermFormatted = '%' . $mysqli->real_escape_string($searchTerm) . '%';
// Récupérer tous les projets correspondant au terme de recherche
$searchQuery = "SELECT * FROM projets WHERE statut=1 AND (titre LIKE ? OR description LIKE ? OR type LIKE ?) ORDER BY id DESC";
$searchStmt = $mysqli->prepare($searchQuery);
$searchStmt->bind_param('sss', $searchTermFormatted, $searchTermFormatted, $searchTermFormatted);
$searchStmt->execute();
$searchResult = $searchStmt->get_result();
$searchProjects = [];
while ($row = $searchResult->fetch_assoc()) {
$searchProjects[] = $row;
}
$total1=count($searchProjects);
$searchStmt->close();
// Récupérer les autres projets non liés à la recherche
$otherQuery = "SELECT * FROM projets WHERE statut=1 AND id NOT IN (SELECT id FROM projets WHERE statut=1 AND (titre LIKE ? OR description LIKE ? OR type LIKE ?)) ORDER BY id DESC";
$otherStmt = $mysqli->prepare($otherQuery);
$otherStmt->bind_param('sss', $searchTermFormatted, $searchTermFormatted, $searchTermFormatted);
$otherStmt->execute();
$otherResult = $otherStmt->get_result();
while ($row = $otherResult->fetch_assoc()) {
$searchProjects[] = $row;
}
$otherStmt->close();
// Diviser les projets en pages
$totalProjects = count($searchProjects);
$totalPages = ceil($totalProjects / $projetsParPage);
$projectsByPage = array_chunk($searchProjects, $projetsParPage);
// Construction de la réponse JSON
$response = [
'projects' => $projectsByPage,
'currentPage' => $page,
'totalPages' => $totalPages,
'searchTerm' => $searchTerm,
'notification' => $total1 == 0 ? "Aucun résultat trouvé pour votre recherche. Affinez votre recherche pour obtenir des résultats plus pertinents." : ""
];
// Fermeture de la connexion
$mysqli->close();
// Renvoyer les projets au format JSON
header('Content-Type: application/json');
echo json_encode($response);
?>
and there the function in the index:
function loadProjects(page, searchTerm) {
fetch(`filtrer.php?recherche=${encodeURIComponent(searchTerm)}&page=${page}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
projectsData = data.projects || []; // Assurez-vous que projectsData est toujours un tableau
currentPage = data.currentPage || 1; // Défaut à la page 1 si la valeur est manquante
totalPages = data.totalPages || 1; // Défaut à 1 si la valeur est manquante
updateProjectGrid(currentPage);
updatePagination(searchTerm);
if (data.notification) {
showNotification(data.notification,"error");
}
})
.catch(error => console.error('Error:', error));
}
Please help!!!
using the inspection tool i found that is the json I receive in localhost: enter image description here
but on the namecheap host, the json is simply empty
I tried to change the way I create the json, but still doesn’t work on my host wich is a Namecheap host. I tried to explore the server configuration to find something but nothing.