Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
Trying to build simple tag system with php

In my database I have a table called topics, in the table there is the tags column where the tags are stored in a comma-separated order (for example $row['tags'] = "tag1,tag2,tag3,...". What I'm trying to do is display related topics depending on the tags that each has. The code below identifies only the single "tag" it finds (for example $row['tags'] ="tag1"). Do you have something to suggest? Thanks in advance.

 <?php
    $result_topic = $conn -> prepare("SELECT * FROM topics WHERE topics.topic_id = :id");
    $result_topic->bindValue(':id',$topic_id, PDO::PARAM_INT);
    $result_topic -> execute();
    while($row = $result_topic->fetch(PDO::FETCH_ASSOC))
    {
$tag = explode(",",$row['tags']);
            for($i = 0; $i < count($tag); $i++){
            $current_tag = $tag[$i];
           $relative_topics = $conn -> prepare("SELECT topic_subject FROM topics WHERE tags LIKE ?");
           $relative_topics -> bindParam(1,$current_tag);
            $relative_topics -> execute();
            $get_relative_topics = $relative_topics -> fetch(PDO::FETCH_ASSOC);
            echo $get_relative_topics['topic_subject'].'<br>';
                                }

       }
?>
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Like expression :

With LIKE you can use the following two wildcard characters in the pattern:
% matches any number of characters, even zero characters.
_ matches exactly one character.

so, you need to use the % wildcard in your query:

$current_tag = "%" . $tag[$i] . "%";
$relative_topics = $conn -> prepare("SELECT topic_subject FROM topics WHERE tags LIKE ?");
over 4 years ago · Santiago Trujillo Denunciar

0

You could try using following SQL query -

$relative_topics = $conn -> prepare("SELECT topic_subject FROM topics WHERE tags LIKE %?%");
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda