Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i create a tagging system using php and mysql?

wondering how i can create a tagging system in php and with a mysql database, my initial thoughts were to create a row in the table where the articles are stored, named tags, and list the tags seperated by commas, but i am not sure how i can create a query that searches for matching tags, i dont want to have to query every article everytime someone clicks a tag. can anyone help?

like image 995
mcbeav Avatar asked Mar 01 '11 21:03

mcbeav


People also ask

How can we create a database using PHP and MySQL?

The basic steps to create MySQL database using PHP are: Establish a connection to MySQL server from your PHP script as described in this article. If the connection is successful, write a SQL query to create a database and store it in a string variable. Execute the query.


1 Answers

You're describing a many-to-many relationship between Articles and Tags. You'd want to use an intermediate junction table to resolve that relationship.

enter image description here

Now, to find all articles that match a selected tag:

SELECT a.article_title
    FROM Tag t
        INNER JOIN Article_Tag_Xref atx
            ON t.tag_id = atx.tag_id
        INNER JOIN Article a
            ON atx.article_id = a.article_id
    WHERE t.tag_name = 'SelectedTagName'
like image 156
Joe Stefanelli Avatar answered Oct 09 '22 04:10

Joe Stefanelli