homepage Welcome to WebmasterWorld Guest from 54.237.71.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help Query Two Tables - PHP/MySQL
Help Query Two Tables - PHP/MySQL
MizzBia




msg:3763886
 12:13 am on Oct 12, 2008 (gmt 0)

Hello,
I am a major PHP newbie. I have an existing site (in Perl). I am trying to create a new page in PHP that will basically query the database and then display those results in a table. I've done fine so far however I am now stuck. Basically, I would like to know how to query 2 separate tables. I have my script setup to grab info from the first table called "articles" but I also need to get some info from a second table called "comments". There is a common denominator between the two tables, however the column name is not the same. In articles I have a column named "entry" and in "comments" it is referred to as "entryid". Inside the "comments" field, there is a "counter" with how many comments each article has. I would like to be able to only show results when the comments table has a NULL in the column of "counter".

So query the info from two tables BUT only show the result if the column in the second table does not have a value (its NULL meaning no comments have been made). Can anyone help me with this or point me in the right direction. This is what I have so far (which only pulls all the data within the first table):


//connect to mysql
mysql_connect(localhost,$username,$password);

//select database
mysql_select_db("main_database");

//select the table
$result = mysql_query("select * from articles");

//grab all the content
while($r=mysql_fetch_array($result))
{

//modify these to match your mysql table columns

$title=$r["title"];
$entry=$r["entry"];
$user=$r["user"];
$date=$r["posted"];
$views=$r["views"];
$type=$r["type"];

//display the row
echo "$title <br> entry id: $entry <br> user id: $user <br> $date <br>type: <br>";
}

Thank you for your time

 

phranque




msg:3763943
 4:10 am on Oct 12, 2008 (gmt 0)

i would try something like this:
SELECT * FROM articles,comments WHERE articles.entry=comments.entryid AND comments.counter IS NOT NULL

however it seems to me like you should have the counter column in the articles table.

MizzBia




msg:3763961
 5:02 am on Oct 12, 2008 (gmt 0)

Hey phranque,
I will try that out and post the results. And yes, I agree about the counter. I'm not the original programmer of my site but I definitely thought the same thing when I looked at the database. Thank you for taking the time to help. I am off to see if that works :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved