I’m looking to see if I can create a directory site in rapidweaver. So SQL database and display search results in my site. I’ve found a few old stacks that handled databases but they seem to be really old and not supported anymore. Has any one found a way to do this? maybe embedding a third party service? - all ideas welcome, Many thanks
You could probably get ChatGPT to write a little bit of code to connect to the database and output the results using some Tailwind code. You could then throw this into a custom component
But you’re absolutely right - it’s quite the investment. The Feeds stack is quite nice though, and if you’re building stuff that needs to dynamically (and safely) access databases, it’s actually a pretty good way to do it.
Buuuut…. if the only goal is to display plain text pulled from a MySQL database based on a search query on the same page (basic knowledge base type content), something like this would work in PHP and style-able in Tailwind as a bonus should user switch to Elements:
<?php
/* ───────────── Settings ───────────── */
const DB_DSN = 'mysql:host=localhost;dbname=your_database;charset=utf8mb4';
const TABLE_NAME = 'documents'; // <- change to your table
const SEARCH_COLUMN = 'content'; // <- change to the text column you want to search
const SELECT_LIMIT = 50; // safety limit
/* ──────────────────────────────────── */
declare(strict_types=1);
header('Content-Type: text/html; charset=utf-8');
function db(): PDO {
// Assuming DB is open to the outside world in read-only mode and requiresno username/password:
$pdo = new PDO(DB_DSN, null, null, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
return $pdo;
}
//making sure only UTF-8 can be used
function h(string $s): string { return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }
$q = trim($_GET['q'] ?? '');
$page = max(1, (int)($_GET['page'] ?? 1));
$offset = ($page - 1) * SELECT_LIMIT;
$results = [];
$total = null;
if ($q !== '') {
// Basic, safe LIKE search; swap to FULLTEXT once indexed.
$sql = sprintf(
'SELECT SQL_CALC_FOUND_ROWS %1$s AS txt
FROM %2$s
WHERE %1$s LIKE :q
ORDER BY %1$s ASC
LIMIT :lim OFFSET :off',
SEARCH_COLUMN,
TABLE_NAME
);
$pdo = db();
$stmt = $pdo->prepare($sql);
$like = "%{$q}%";
$stmt->bindParam(':q', $like, PDO::PARAM_STR);
$stmt->bindValue(':lim', SELECT_LIMIT, PDO::PARAM_INT);
$stmt->bindValue(':off', $offset, PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll();
// Total rows for pagination
$total = (int)$pdo->query('SELECT FOUND_ROWS()')->fetchColumn();
$pages = max(1, (int)ceil($total / SELECT_LIMIT));
} else {
$pages = 1;
}
?>
//Checking if Dan actually read this code :)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Search</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="min-h-screen bg-slate-50 text-slate-900">
<div class="max-w-3xl mx-auto p-6">
<h1 class="text-2xl font-semibold mb-4">Search</h1>
<!-- Search box -->
<form method="get" class="flex items-center gap-2 mb-6">
<label for="q" class="sr-only">Search</label>
<input
id="q"
name="q"
type="text"
value="<?= h($q) ?>"
placeholder="Type to search…"
class="w-full rounded-xl border border-slate-300 bg-white px-4 py-3 outline-none focus:ring-4 focus:ring-indigo-100 focus:border-indigo-400"
/>
<button
type="submit"
class="rounded-xl bg-indigo-600 px-4 py-3 font-medium text-white hover:bg-indigo-700 active:bg-indigo-800 focus:outline-none focus:ring-4 focus:ring-indigo-200"
>Search</button>