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 trialjason chan
31,009 PointsI don't understand the if statement in mike4shirts
<?php
function get_product_single($sku) {
require(ROOT_PATH . "inc/database.php");
try {
$results = $db->prepare("SELECT name, price, img, sku, paypal FROM products WHERE sku = ?");
$results->bindParam(1,$sku);
$results->execute();
} catch (Exception $e) {
echo "Data could not be retrieved from the database.";
exit;
}
$product = $results->fetch(PDO::FETCH_ASSOC);
if ($product === false) return $product;
$product["sizes"] = array();
try {
$results = $db->prepare("SELECT product_sku, size_id, size FROM `products_sizes` INNER JOIN sizes ON products_sizes.size_id = sizes.id WHERE product_sku = ? order by `order`");
$results->bindParam(1,$sku);
$results->execute();
} catch (Exception $e) {
echo "data could not be retrieved";
exit;
}
while( $row = $results->fetch(PDO::FETCH_ASSOC)) {
$product["sizes"][] = $row["size"];
}
return $product;
}
I don't understand the if product === false why does the product run?
1 Answer
Ted Sumner
Courses Plus Student 17,967 PointsIf the fetch fails, then it returns false. So on failure, the method returns $product, which is false. That goes to shirt.php, which has the following code:
<?php
if (empty($product)) {
header("Location: " . BASE_URL . "shirts/");
exit();
}
I am guessing that false is also empty, so the site is redirected back to shirts instead of the individual page.
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 PointsEdited to format code by adding <?php in the code block.