Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help Query Two Tables - PHP/MySQL

Help Query Two Tables - PHP/MySQL



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

5+ Year Member

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

//select database

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

//grab all the content

//modify these to match your mysql table columns


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

Thank you for your time


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

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

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.


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

5+ Year Member

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 :)

Featured Threads

Hot Threads This Week

Hot Threads This Month