Jump to content

Nested SQL Statements


seran128

Recommended Posts

ok I have three tables the first table holds the category Name
Somthing like

tbl_category

CategoryID
Category Name

The second table holds news information Something like

tbl_news

NewsID (int)
News Title (varchar80)
News Content (blob)

The third table joins the two tables

tbl_news_join_category

ID (INT)
NewsID (INT)
CategoryID (INT)

this way I can have a news article belong to many different Categories ok

my function


[code]function news(){

global $connection;
$sql="select * from tbl_category";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$news .= "<table width='200' border='0'>
    <tr>
      <td>" . stripslashes($row['category_name']) . "</td>

    </tr>
    <tr>
      <td></td>
    </tr>
  </table>";
}
return $news
}[/code]

This returns the category name as it should now what I would like to do is do a query after the

[code]<td>" . stripslashes($row['category_name']) . "</td>[/code]

and display all the news titles that belong to that category

then continue on
with the rest of the code

[code]</tr>
    <tr>
      <td></td>
    </tr>
  </table>";
}
return $news
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/27184-nested-sql-statements/
Share on other sites

[code]<?php
function news(){

global $connection;
$sql="select * from tbl_category";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$news .= "<table width='200' border='0'>
    <tr>
      <td>" . stripslashes($row['category_name']) . "</td>";
$sqls = mysql_query("SELECT * FROM tbl_news WHERE category='$row[category_name]'") or die(mysql_error());
while($rows=mysql_fetch_array($sqls)) {
echo $rows['column_name_from_tbl_news'];
}
echo"
    </tr>
    <tr>
      <td></td>
    </tr>
  </table>";
}
return $news
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/27184-nested-sql-statements/#findComment-124324
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.