Search code examples
phpmysqlfull-text-searchprepared-statementfulltext-index

Does using FULLTEXT in MySQL require for me to use a different syntax in PHP to retrieve the results?


Here's my code:

$terminosBuscados = 'Vender la porteria'

$x = $conectarDB->prepare(" 
    SELECT DISTINCT 
    titulo,
    FROM_UNIXTIME(fecha, '%d-%m-%Y') AS fecha,
    cuerpo,
    tipoContenido,
    autor
    FROM searchIndex 
    WHERE match(titulo) 
    AGAINST (' ? ' IN BOOLEAN MODE)
    ORDER BY contenidoID DESC
    ");
$x->bindParam(1, $terminosBuscados);
$x->execute();
$y = $x->fetchAll(PDO::FETCH_ASSOC);

This is showing no results at all ($y returns null), when trying the query itself in phpMyAdmin does work and show results.

I've just added a FULLTEXT index into the table and adapted my working code to use that index.


Solution

  • For mysql ' ? ' is a string, and will not be used as palce holfer.

    If you need the spaces use CONCAT(' ',?,' ')

    $x = $conectarDB->prepare(" 
        SELECT DISTINCT 
        titulo,
        FROM_UNIXTIME(fecha, '%d-%m-%Y') AS fecha,
        cuerpo,
        tipoContenido,
        autor
        FROM searchIndex 
        WHERE match(titulo) 
        AGAINST (CONCAT(' ',?,' ') IN BOOLEAN MODE)
        ORDER BY contenidoID DESC
        ");
    $x->bindParam(1, $terminosBuscados);
    $x->execute();
    $y = $x->fetchAll(PDO::FETCH_ASSOC);