Tags are stored in the database by linking posts (table qa_posts) to words (table qa_words) with an associative table qa_posttags. To restrict your query to a particular tag you need to join the posts table to the words table via that associative table.
SELECT q.postid
FROM qa_posts q
INNER JOIN qa_posttags t ON q.postid=t.postid
INNER JOIN qa_words w ON t.wordid=w.wordid
WHERE w.word='tagname';
So your statement should probably look like this:
qa_db_read_one_assoc(qa_db_query_sub('SELECT * FROM ^posts q INNER JOIN ^posttags t ON q.postid=t.postid INNER JOIN ^words w ON t.wordid=w.wordid WHERE q.type="Q" AND w.word=$ ORDER BY rand() LIMIT 1;', $tagname)); |