Solar Posted April 28, 2010 Share Posted April 28, 2010 <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="login"; // Database name $tbl_name="forum_titles"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE cat='general' ORDER BY fid"; // OREDER BY id DESC is order result by descending $result=mysql_query($sql); $link = mysql_connect("localhost", "root", ""); mysql_select_db("login", $link); $result2 = mysql_query("SELECT fid FROM forum_question WHERE fid='$fid'", $link); $num_rows = mysql_num_rows($result2); ?> <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td width="53%" align="center" bgcolor="#E6E6E6"><strong>General</strong></td> </tr> <?php while($rows = mysql_fetch_array($result, $result2) ){ // Start looping table row ?> <tr> <td bgcolor="#FFFFFF"> <a href="forums.php?forum=<? echo $rows['fid']; ?>"> <? echo $rows['forum']; ?></a><BR><span class="style1"> <? echo $rows['desc']; ?> - <?php echo "$num_rows"; ?> </span> </td> </tr> <?php // Exit looping and close connection } mysql_close(); ?> What I would like to do is have database-forum_question (Forum Topics) Show how many topics are in that specific database-forum_title (Forum Name). So That being said. My databases look like this; Database Forum_Title fidforumdesccat 1GeneralBlah Blah...1 2SupportBlah Blah...2 Database Forum_Question idfidquestiondetailcat 11General TestBlah Blah...1 22Support TestBlah Blah...2 What I want; For example; General - 4 Topics Support - 2 Topics Anything - 0 Topics I think I am on the right track.. But I know for sure that this following doesn't help. while($rows = mysql_fetch_array($result, $result2) ){ // Start looping table row How would I be able to fix this up? *There is no error, it just results as Zero as the two databases aren't connecting properly* Link to comment https://forums.phpfreaks.com/topic/200091-2-rows/ Share on other sites More sharing options...
hitman6003 Posted April 29, 2010 Share Posted April 29, 2010 JOIN the tables and use a "GROUP BY" statement... SELECT fid, forum, desc, COUNT(fid) FROM forum_titles ft LEFT JOIN forum_questions fq ON fq.fid = ft.fid GROUP BY fid Link to comment https://forums.phpfreaks.com/topic/200091-2-rows/#findComment-1050262 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.