Add logging for SQL queries and Pokémon data retrieval in PHP and JavaScript

This commit is contained in:
vista-man
2025-03-25 14:12:06 +01:00
parent 2c79be3ef4
commit 78e745752a
2 changed files with 16 additions and 0 deletions

View File

@@ -34,12 +34,14 @@ if (isset($_GET['id'])) {
LEFT JOIN stats st ON p.id = st.pokemon_id LEFT JOIN stats st ON p.id = st.pokemon_id
WHERE p.id = $id WHERE p.id = $id
GROUP BY p.id"; GROUP BY p.id";
logMessage("Executing query: $sql");
$result = $conn->query($sql); $result = $conn->query($sql);
if ($result->num_rows > 0) { if ($result->num_rows > 0) {
$pokemon = $result->fetch_assoc(); $pokemon = $result->fetch_assoc();
$pokemon['types'] = explode(',', $pokemon['types']); $pokemon['types'] = explode(',', $pokemon['types']);
$pokemon['abilities'] = explode(',', $pokemon['abilities']); $pokemon['abilities'] = explode(',', $pokemon['abilities']);
logMessage("Query result: " . json_encode($pokemon));
echo json_encode($pokemon); echo json_encode($pokemon);
} else { } else {
logMessage("No Pokémon found for ID: $id"); logMessage("No Pokémon found for ID: $id");
@@ -47,12 +49,14 @@ if (isset($_GET['id'])) {
} }
} else { } else {
$sql = "SELECT * FROM pokemon"; $sql = "SELECT * FROM pokemon";
logMessage("Executing query: $sql");
$result = $conn->query($sql); $result = $conn->query($sql);
$pokemons = []; $pokemons = [];
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$pokemons[] = $row; $pokemons[] = $row;
} }
logMessage("Query result: " . json_encode($pokemons));
echo json_encode($pokemons); echo json_encode($pokemons);
} }

View File

@@ -14,6 +14,7 @@ document.addEventListener("DOMContentLoaded", () => {
if (cachedData && cacheTimestamp && (Date.now() - cacheTimestamp) < CACHE_DURATION) { if (cachedData && cacheTimestamp && (Date.now() - cacheTimestamp) < CACHE_DURATION) {
allPokemons = JSON.parse(cachedData); allPokemons = JSON.parse(cachedData);
console.log("Loaded Pokémon data from cache:", allPokemons);
displayPokemons(allPokemons); displayPokemons(allPokemons);
} else { } else {
fetchPokemons(); fetchPokemons();
@@ -21,6 +22,7 @@ document.addEventListener("DOMContentLoaded", () => {
}); });
function fetchPokemons() { function fetchPokemons() {
console.log("Fetching Pokémon data from server...");
fetch(`./get-pokemon.php`) fetch(`./get-pokemon.php`)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
@@ -28,6 +30,7 @@ function fetchPokemons() {
allPokemons = data; allPokemons = data;
localStorage.setItem("pokemons", JSON.stringify(allPokemons)); localStorage.setItem("pokemons", JSON.stringify(allPokemons));
localStorage.setItem("pokemons_timestamp", Date.now().toString()); localStorage.setItem("pokemons_timestamp", Date.now().toString());
console.log("Fetched and cached Pokémon data:", allPokemons);
displayPokemons(allPokemons); displayPokemons(allPokemons);
} else { } else {
console.error("Unexpected response format:", data); console.error("Unexpected response format:", data);
@@ -40,9 +43,13 @@ function fetchPokemons() {
async function fetchPokemonDataBeforeRedirect(id) { async function fetchPokemonDataBeforeRedirect(id) {
try { try {
console.log(`Fetching data for Pokémon ID ${id} before redirect...`);
const response = await fetch(`./get-pokemon.php?id=${id}`); const response = await fetch(`./get-pokemon.php?id=${id}`);
const text = await response.text(); const text = await response.text();
console.log(`Response for Pokémon ID ${id}:`, text); console.log(`Response for Pokémon ID ${id}:`, text);
if (!text) {
throw new Error("Empty response from server");
}
const pokemon = JSON.parse(text); const pokemon = JSON.parse(text);
if (pokemon.error) { if (pokemon.error) {
@@ -57,6 +64,7 @@ async function fetchPokemonDataBeforeRedirect(id) {
} }
function displayPokemons(pokemons) { function displayPokemons(pokemons) {
console.log("Displaying Pokémon data:", pokemons);
listWrapper.innerHTML = ""; listWrapper.innerHTML = "";
pokemons.forEach((pokemon) => { pokemons.forEach((pokemon) => {
@@ -90,6 +98,7 @@ function displayPokemons(pokemons) {
} }
function lazyLoadImages() { function lazyLoadImages() {
console.log("Initializing lazy loading for images...");
const images = document.querySelectorAll('.img-wrap img[data-src]'); const images = document.querySelectorAll('.img-wrap img[data-src]');
const config = { const config = {
root: null, root: null,
@@ -116,6 +125,7 @@ function preloadImage(img) {
if (!src) { if (!src) {
return; return;
} }
console.log("Preloading image:", src);
img.src = src; img.src = src;
} }
@@ -123,6 +133,7 @@ searchInput.addEventListener("keyup", handleSearch);
function handleSearch() { function handleSearch() {
const searchTerm = searchInput.value.toLowerCase(); const searchTerm = searchInput.value.toLowerCase();
console.log("Handling search with term:", searchTerm);
let filteredPokemons; let filteredPokemons;
if (numberFilter.checked) { if (numberFilter.checked) {
@@ -151,6 +162,7 @@ const closeButton = document.querySelector(".search-close-icon");
closeButton.addEventListener("click", clearSearch); closeButton.addEventListener("click", clearSearch);
function clearSearch() { function clearSearch() {
console.log("Clearing search input and displaying all Pokémon...");
searchInput.value = ""; searchInput.value = "";
displayPokemons(allPokemons); displayPokemons(allPokemons);
notFoundMessage.style.display = "none"; notFoundMessage.style.display = "none";