I'm trying to create a document (and a form that can modify or choose the info used in the document) from 2 tables in the same db. The tables are setup like this:
Sections table
CREATE TABLE sections (section_id int, section_title text, primary key (section_id))
Articles table
CREATE TABLE articles (article_id int, section_id int, article_text text, article_title text, primary key (article_id), foreign key (section_id) references sections(section))
Here's some further info on how they are to be connected:
sections table is:
section_id section_title
1 overview
2 first section
3 second section
4 forth section
articles table is:
section_id article_title article_text
1 first article This is the first article - it's in the first section
1 second article This is the second article - it's in the first section
2 third article This is the third article - it's in the second section
2 forth article This is the forth article - it's in the second section
3 fifth article This is the fifth article - it's in the third section
What I am trying to accomplish is to create a form that will allow me to modify the article title, text and it's section (and it change the articles table, while also allowing me to modify the section title itself and it modify the sections table.
Then, I'm going to create a different form that will allow me to choose the articles to display and then it will compile the article correctly when the form is submitted.
Presently, I understand that the two tables need to be joined; however, when I try to display the combined info, I get nothing but errors. Here's the coding so far:
<!-- Include settings.php -->
<?
include 'settings.php';
?>
<!-- Connect to mysql -->
<?
$connection = mysql_connect("$mysql_host", "$mysql_user", "$mysql_password");
?>
<!-- Select the correct database -->
<?
$selected_db = mysql_select_db($mysql_db, $connection);
if (!$selected_db)
{
die ('Can NOT select '. $mysql_db . ' database because ' . mysql_error());
}
?>
<!-- Build query string -->
<?
$query = 'SELECT section_id, article_title, article_text FROM articles JOIN sections ON articles.section_id=sections.section_id ORDER BY sections.section_id, articles.article_title';
$result = mysql_query($query, $connection);
$num_rows = mysql_num_rows($result);
?>
The error I get is:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in line 12
I have tried everything I can think of and have come up short all the time. I am a BIG fan of the RTFM approach and I can successfully do an individual table query just fine. I'm missing something in regards to the second table, how to connect them and how to do the queries with them in order to get what I need.
Thanks for the assist!
Sincerely,
wrstrong