tags:

views:

272

answers:

2

I have a table called cms_page in which each page has an ID. I want to pull all the information from this table and all the information from cms_page_part table where page_id is equal to the ID i send to the page...confusing i know but here is my attempt:

require '../../config.php';
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);

$id = (int)$_GET['id'];
//$q = $conn->query("SELECT * FROM cms_page WHERE id=$id"); 
$q = $conn->query("SELECT cms_page.id, cms_page.title, cms_page.slug, cms_page_part.* FROM cms_page LEFT JOIN cms_page_part ON cms_page_part.page_id=cms_page.id WHERE cms_page.id = $id"); 

$project = $q->fetch(PDO::FETCH_ASSOC);

Any help figuring out how to join these would be appreciated.

A: 
SELECT id, title, slug, cms_page_part.* FROM cms_page JOIN cms_page_part ON cms_page.id = cms_page_part.page_id WHERE cms_page.id=$id

Edited to give you an idea of what I said in my comment.

Do something like...

SELECT * FROM cms_page WHERE id=1 LIMIT 1

Then check to see if the query returned a result, if it doesn't then there is no point in trying to find "parts" now is there?

Do another query with SELECT * FROM cms_page_parts WHERE page_id=1 and then you'll have all the "parts" you need to use within that page. Hopefully that answers your question.

William
Didnt seem to work...should i removecms_page_part.*
Andy
Still no joy sorry
Andy
Can you post the SQL error you're getting?
William
the one i slashed out brings me the title nicely but the errors are supressed on my page and i dont know how to turn them back on :(
Andy
Just so I got this right, both tables has a field named page_id, correct? And the page ID you're testing this out on, has a row in both tables with the same page_id?
William
no sorry...in cms_page its "id" and in cms_page_part its "page_id"
Andy
I just updated my query, try that out.
William
I just updated my original post. THis works :@) But i have another issue. in cms_page_part there are different "parts" with the same page id. Do you know how i can access each one specifically?Thanks for your help.
Andy
If there are more than one, then instead of doing a JOIN I'd just do two query's. Do one select on the cms_page table, then after that if a record was found, do another query to get all the "parts" and loop through them and use them however you need. I actually have to get going for awhile, hopefully that helps!
William
tat does help alot, thanks!
Andy
A: 

$q = $conn->query("SELECT cms_page.id, cms_page.title, cms_page.slug, cms_page_part.* FROM cms_page LEFT JOIN cms_page_part ON cms_page_part.page_id=cms_page.id WHERE cms_page.id = $id");

Andy