php - using multiple mysql query loop inside each other -


i have big trouble using multiple queries inside each other. , searched not find needed! problem. plz me.

i have table named "cat" , titles mysql , it's ok. have other table named works has field named "cat_id" field connect's data of both tables. works table has field named "years" , it's year of production of artwork.

cat >> { id , title , ... } works >> { id , title , url , year , cat_id , ... }

i need categorize gallery of works year, made code this:

<?php $result = q("cat"); while($row = mysqli_fetch_array($result)) { ?>   <div>     <h2><a href="artwork.php?id=<?php echo $row['id'] ?>"> <?php echo $row['title']; ?></a></h2>     <span>     <?php       $sql = "select distinct year works cat=".$row['cat_id'];       $yresult = mysql_query($sql);       while ($yrow = mysql_fetch_row($yresult))        {         //and print anchor here wont loop @ all!                                }                              ?>     </span>   </div> <?php } ?> 

the q() function connects db , run query. line works pretty well. wont enter inner while @ all! checked , saw $yrow totally empty(by empty() of php)! can it? seems mysql can not execute second query @ all! solution must in 2 ways think. 1. make mysql execute second query. 2. change first query in way get's years distinctly , read them mysql_fetch_array , put years anchor via each.

plz me.

instead of having multiple loops, use inner join:

select distinct works.year cat inner join works on (cat.id = works.cat_id) 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -