problem in decreasing page's queries

Posted by Mac Taylor on Stack Overflow See other posts from Stack Overflow or by Mac Taylor
Published on 2010-05-26T12:47:52Z Indexed on 2010/05/26 12:51 UTC
Read the original article Hit count: 190

Filed under:
|

hey guys i have a tag table in my php/mysql project that looks like this

Table name : bt_tags

Table fileds : tid,tag

and for every story rows there is a filed named : tags

Table name: stories

table filed : tags

that saved in this field as ids

1 5 6

space between them

now problem :

when using while loop to fetch all fields in story table , the page uses 1 query to show every stories' detail but for showing tag's names , i should query another table to find names , we have ids stored in story table

now i used for loop between while loop to show tag names but im sure there is a better way to decrease page queries

how can i improve this script and show tag's names without using *for loop ?*

$result = $db->sql_query("SELECT *  FROM ".STORY_TABLE." ");

while ($row = $db->sql_fetchrow($result)) {

    //fetching  other $vars ----

    $tags_id = explode(" ",$row['tags']);
    $c = count($tags_id);
    for($i=1;$i<$c-1;$i++){
        list($tag_name,$slug) =
        $db->sql_fetchrow($db->sql_query(
        'SELECT `tag`,`slug` FROM `bt_tags` WHERE `tid` = "'.tags_id[$i].'" LIMIT 1'
        ));
        $sow_tags       =  '$tag_name,';
    }

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql