swatisonee Posted July 1, 2008 Share Posted July 1, 2008 Hi, I'm sure i'm missing something but cannot figure out what it is. In the foll. code i need ORDER BY to work on the 2nd sql statement but when the script runs, it doesnt. By default it sorts on the primary key of the 1st sql statment. What should I change to ensure that Order By works on the 2nd sql stmt. Or is there a simpler way of coding by combining the 2 sql statements to get me the required result ? $sql11 = "SELECT * FROM `TableA` WHERE `Eid` = $group AND `Month` = $month AND `Year` = $year " ; // echo $sql11; $result11 = mysql_query($sql11); if ($myrow11 = mysql_fetch_array($result11)) { do { $sid = $myrow11["Sid"]; $sql11a = "SELECT * FROM `TableB` WHERE `Sid` ='$sid' ORDER BY `name` asc "; // echo $sql11a; $result11a = mysql_query($sql11a); $myrow11a = mysql_fetch_array($result11a) ; Thanks ! Link to comment https://forums.phpfreaks.com/topic/112708-order-by-on-2-sql-stmts/ Share on other sites More sharing options...
phpSensei Posted July 1, 2008 Share Posted July 1, 2008 Try this <?php $query = mysql_query("SELECT * FROM `TableA` WHERE `Eid` = '$group' AND `Month` = '$month' AND `Year` = '$year' ")or die(mysql_error()) ; $row = mysql_fetch_array($query); if($row){ $sid = $row['Sid']; $query2 = mysql_query("SELECT * FROM `TableB` WHERE `Sid` ='$sid' ORDER BY `name` asc ") or die(mysql_error()); $row2 = mysql_fetch_array($query2); } ?> Link to comment https://forums.phpfreaks.com/topic/112708-order-by-on-2-sql-stmts/#findComment-578813 Share on other sites More sharing options...
swatisonee Posted July 1, 2008 Author Share Posted July 1, 2008 thanks for the tip but it didnt work. it just generates the 1st record and doesnt output the entire table. Here's the entire script to get an idea of what i'm trying to achieve. Thanks <? $sql11 = "SELECT * FROM `TableA` WHERE `Eid` = $group AND `Month` = $month AND `Year` = $year " ; // echo $sql11; $result11 = mysql_query($sql11); if ($myrow11 = mysql_fetch_array($result11)) { do { $sid = $myrow11["Sid"]; $sql11a = "SELECT * FROM `TableB` WHERE `Sid` ='$sid' ORDER BY `name` asc "; // echo $sql11a; $result11a = mysql_query($sql11a); $myrow11a = mysql_fetch_array($result11a) ; $name = $myrow11a["name"]; $a = $myrow11a["INMCC"]; $b = $myrow11a["CRIM"]; $c = $myrow11a["Bity"]; $sqlx= "SELECT Sum(Total) FROM TableA WHERE `Eid` ='$group' AND (Month =$month AND Year=$year) "; //echo $sqlx; // $resultx = mysql_query ($sqlx) or die (mysql_error()); if ($myresult = mysql_result($resultx,0)) // //{ printf("<tr><td> <font size=2 face=Tahoma color=black>%s<td> <font size=2 face=Tahoma color=black>%s<td> <font size=2 face=Tahoma color=black>%s<td> <font size=2 face=Tahoma color=black>%s<td> </tr>", $name, $a, $b, $c); //} } while ($myrow11 = mysql_fetch_array($result11)); } ?> </table> <tr><p> <font size="2"face="Tahoma"><b> <?php echo "Total Value $"; echo $myresult; ?> </b> </tr> ?> Link to comment https://forums.phpfreaks.com/topic/112708-order-by-on-2-sql-stmts/#findComment-578851 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.