Jump to content

Search tags from MySQL database


Genosuki
Go to solution Solved by Barand,

Recommended Posts

Hello, so i made table with Tags row which is Text(126).. and idea is to put text like "PHP, MySQL, Forum, Blog" etc etc...

I have problem searching tags my search code:

$requestedtag = $con->real_escape_string($_GET['tag']);

$sql = "SELECT * FROM images WHERE tags = '$requestedtag' ORDER BY ID DESC LIMIT 12";
		$result = $con->query($sql);

		if ($result->num_rows > 0) 
		{
		    while($row = $result->fetch_assoc()) {
		    	$image_id = $row["ID"];
		    	$image_title = $row["Title"];
		    	$image_url = $row["ImgURL"];
		    	$image_tags = $row["Tags"];
		    	echo '<div class="col-3"><div class="thumb"><a href="/image.php?id='.$image_id.'" title="'.$image_title.'"><img src="'.$image_url.'"></img></a></div></div>';
			}
		}

But it doesnt return anything. I think the problem is that row["Tags"]; is divided by "," from tags and i should split it somehow but i dont really know what to do...
Link to comment
Share on other sites

One of the first rules of database design and normalization is to never put the same data items in one field.  Your tags are each one a separate piece of data.  They are therefore equal.  And because of that they should be stored separately into a separate table as separate rows.

 

So for each combination of Id/Title/Url you should have but one record with a tag.  If there are more tags with those same attributes then you need to have a separate record with each new tag.  Then when you are looking for the tags for a specific combination of Id/Title/Url you run a query that pulls all the rows with those particular values.  You can then loop thru those result rows and capture each of the tags.  Output them as you wish.

Link to comment
Share on other sites

  • Solution

So you would have two tables and use a join to match on tag

+-----+-------------+------------+
| ID  | title       | imageURL   |
+-----+-------------+------------+
|  1  |  abc        | /im/abc.jpg|
|  2  |  xyz        | /im/xyz.jpg|
+-----+-------------+------------+
   |
   |
   +---------------+
                   |
                +--------+-------------+
                |imageID |   tag       |
                +--------+-------------+
                |  1     | animal      |
                |  1     | dog         |
                |  1     | snow        |
                |  2     | mountains   |
                |  2     | snow        |
                +--------+-------------+

Example

SELECT i.ID, i.title
FROM images i
  JOIN tags t ON i.ID = t.imageID
WHERE t.tag = 'snow'
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.