Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dave Faliskie
17,793 PointsCan't seem to get the Relationship Tables in MySQL to work.
I'm following along with the Using PHP with mySQL. everything worked perfectly until I tried to get the "shirt sizes" from the relationship table. I rechecked my code and to me it looks correct. did anyone else have this problem? is there anything that needs to be changed that I could possibly be missing I am using the same concepts but for a site that will display different companies instead of shirts, so the names of the variables are different but the code is basically the same. here is my "get_company_single" function. (similar to get_product_single)
<?php
function get_company_single($id){
include("include/companies_database.php");
try{
$results = $db->prepare("SELECT id, name, image, location FROM companies WHERE id = ?");
$results->bindParam(1,$id); // replaces the 1st(only) ? with $id
$results->execute();
} catch (Exception $e){
echo "Error! from the get_company_single function";
exit;
}
$company = $results->fetch(PDO::FETCH_ASSOC);
if($company === false){
return $company;
}
// this part will link the job titles to the company.
$company["titles"] = array();
try{
$results = $db->prepare("SELECT title FROM companies_titles INNER JOIN titles ON companies_titles.title_id = titles.id WHERE company_id = ? ORDER BY `order`");
$results->bindParam(1,$id);
$results->execute();
}catch(Exception $e){
echo "ERROR! look at the get_company_single function, at the part where linking the job titles.";
exit;
}
// loads all job titles while there are job titles. puts them into $row.
while($row = $results->fetch(POD::FETCH_ASSOC)){
$company["titles"][] = $row["title"];
}
return $company;
}
?>
everything worked until I put in the 2nd try/catch block. I have 3 tables. (table: name of rows) companies: id, name, image, location titles: id, title, order companies_titles: company_id, title_id
if anyone sees any mistake I made, or knows why this will not load the page that is calling the get_company_single function I would greatly appreciate their feedback.
Thanks!
6 Answers
Christopher Hall
9,052 PointsOh, I got it! It was a typo on the while line. POD::FETCH_ASSOC instead of PDO::FETCH_ASSOC.
Christopher Hall
9,052 PointsYour SQL has no syntax problems, so the error must be somewhere else. See if you can turn on PHP error reporting (assuming you're on a development server - not a good idea for a production server) so you can get a better idea of what's going wrong.
Christopher Hall
9,052 PointsThe result of the company assignment will either be 'false' or an associative array of company information. Try using the print_r() function after that line to see exactly what it is. If it is false then you're returning that instead of the associative array. That could cause problems with other operations if you're assuming that an associative array will always be returned. Try testing it with both an ID that exists and one that doesn't.
Dave Faliskie
17,793 PointsThanks for responding. I Have a link that connects to a company.php page (would act like the shirt.php page). everything works fine if i comment out everything below the
if($company === false)
While commented, It displays the select box that should contain the job titles (shirt sizes) except its obviously empty.
When the code is not commented and is run in full as shown in OP the link shows nothing. Nothing at all loads... So I would think there is something wrong with this section of the code
if($company === false){
return $company;
}
this part will link the job titles to the company.
$company["titles"] = array();
try{
$results = $db->prepare("SELECT title FROM companies_titles INNER JOIN titles ON companies_titles.title_id = titles.id WHERE company_id = ? ORDER BY `order`");
$results->bindParam(1,$id);
$results->execute();
}catch(Exception $e){
echo "ERROR! look at the get_company_single function, at the part where linking the job titles.";
exit;
}
// loads all job titles while there are job titles. puts them into $row.
while($row = $results->fetch(POD::FETCH_ASSOC)){
$company["titles"][] = $row["title"];
}
I have no idea what it could be tho hahaha.... if anyone can help me I would be very grateful
Dave Faliskie
17,793 PointsAre you talking about the while statement, because actually everything works without the while statement... do you know if there is something wrong with this?
while($row = $results->fetch(POD::FETCH_ASSOC)){
$company["titles"][] = $row["title"];
}
or do you know if there is another way to write a while statement that will add an array with the "title" results and key of ["titles"] to the existing array $company.
I believe that is what this while statement is trying to do but I could be wrong. Thanks
Dave Faliskie
17,793 PointsTHANK YOU!!! that was it! I cant thank you enough, I've spent all day trying to figure this out.
Christopher Hall
9,052 PointsDon't you hate it when the problem was something really minor like a typo? For the future, figure out how to display PHP errors, as it would have pointed you directly to that line. Without them you're sort of flying blind. :)