Database

PHP PDO Search System

Basic MySQL article search with a minimum query length and prepared statement.

Overview

Basic MySQL article search with a minimum query length and prepared statement.

What this script does

This starter demonstrates the core implementation pattern. Adapt database fields, configuration and error handling to your application.

Source code

<?php
declare(strict_types=1);
function searchArticles(PDO $pdo,string $query,int $limit=20): array {
    $query=trim($query); if (mb_strlen($query)<3) return [];
    $limit=min(50,max(1,$limit));
    $s=$pdo->prepare('SELECT id,title,slug,excerpt FROM articles WHERE status=:status AND (title LIKE :q OR excerpt LIKE :q) ORDER BY published_at DESC LIMIT :lim');
    $s->bindValue(':status','published'); $s->bindValue(':q','%'.$query.'%'); $s->bindValue(':lim',$limit,PDO::PARAM_INT);
    $s->execute(); return $s->fetchAll(PDO::FETCH_ASSOC);
}

Download starter source: Download PHP PDO Search System source

Installation and customization

  1. Review the code and required PHP extensions.
  2. Configure database and storage settings outside the public web root where applicable.
  3. Add authorization, rate limiting and logging for public-facing endpoints.
  4. Test valid, invalid and boundary inputs before deployment.

> This is a learning starter, not a complete production application. Apply the security and deployment requirements of your project.