prepare("SELECT id, title FROM products WHERE title LIKE CONCAT(?, '%') LIMIT 10"); $stmt->bind_param('s', $query); // Execute the statement and get the result if ($stmt->execute()) { $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $suggestions[] = [ 'title' => htmlspecialchars($row['title']), // Prevent XSS 'url' => 'product_details?id=' . urlencode($row['id']) ]; } } else { // Handle SQL execution error (optional, you could log the error) http_response_code(500); echo json_encode(['error' => 'An error occurred while fetching suggestions.']); exit; } $stmt->close(); } } echo json_encode(['suggestions' => $suggestions]); // Close the database connection $conn->close(); ?>