Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:22:40+00:00 2026-05-23T16:22:40+00:00

use PHP and MYsql. I have 2 tables like table ‘tag’: has field ‘id’

  • 0

use PHP and MYsql. I have 2 tables like

  • table ‘tag’: has field ‘id’ and ‘tag’ (‘tag’ is varchar)

  • table ‘entry’: has fields ‘tags’ and other fields (‘tags’ is varchar that stores many words as tag, each word is separated by comma. Then I use function ‘explode’ to extract tags from string to store in array ‘$tags’. All tags from table ‘entry’ are stored in table ‘tag’.)

What I want is to select all ‘tag’s from table ‘tag’ which are tags in the same string of ‘tags’ of table ‘entry’ that has a given tag (variable $tag). But no idea, I just try to SELECT tag from table ‘entry’ which string in ‘tags’ contains a specified tag (variable $tag).My current code is

$count=0;
$sql="SELECT tags FROM entry WHERE tag LIKE '%$tag%'";
$result = mysql_query($sql);
while($data=mysql_fetch_array($result)){
  if(mysql_num_rows($result)!=0){
    $tags1=explode(',',$data['tags']);
    for($loop=0; $loop<=count($tags1)-1; $loop++){
      if(!in_array($tags1[$loop], $tags2)){
        $count+=1; 
        $tags2[$count]=$tags1[$loop];
      }
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:22:40+00:00Added an answer on May 23, 2026 at 4:22 pm

    The difficulties you are having solving this problem all stem from the fact that you’re storing a list of tags in a single column.

    You really need to add an intersection table, to contain the data for the many-to-many relationship between tags and entries. This is the proper way to design a relational database, and many SQL queries will become more efficient if you do that.

    For example, to get all the tags from an entry that matches tag $tag:

    <?php
    
    $sql = "SELECT t1.tag FROM tag AS t1
    INNER JOIN entries_tags AS e1 ON e1.tag = t1.id
    INNER JOIN entries_tags AS e2 ON e2.entry = e1.entry
    INNER JOIN tag AS t2 ON t2.id = e2.tag
    WHERE t2.tag = ?";
    
    $stmt = $pdo->prepare($sql);
    $result = $stmt->execute( array($tag) );
    
    $data = $result->fetchAll();
    $count = count($data);
    

    I wrote a chapter to detail the practical problems caused by this design in my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use php to display a table of data drawn from my mysql database.
Using php and mysql, I have a table subdomains that contains subdomains for my
i have a table that looks like this: Field: msg_sent_datetime Type: datetime yet when
I have a social network similar to myspace but I use PHP and mysql,
I'm using mysql/php/apache . I have the following situation: 2 tables where I need
I have two tables in mySQL, notably accounts and subscriber_data. I want to use
I am trying to use PHP and MySQL's Create Table Select between two different
I have two MySQL tables, subjects and books, and a third table to define
I have three tables: table "package" ----------------------------------------------------- package_id int(10) primary key, auto-increment package_name varchar(255)
I don't use php or mysql but I am trying to upgrade a phpbb

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.