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

PHP CRUD Operations with PHP Creating Records Reading Data

CRUD operations with php

Complete the function to return a PDOStatement object containing the podcasts from a database. The function get_podcasts has been started for you, and contains a connection to the database. The connection object is named $db. In the database is a table named 'podcasts'.

index.php
<?php
function get_podcasts() {
    include 'connection.php';
  try{
  return $db->query('SELECT podcats_id FROM podcasts');
  } catch (Exception $e){
  echo "Error!" . $e->getMessage() . "<br/>";
    return array ();
  }
$sql = 'SELECT podcats_id FROM podcasts';
    //add code here
try{
 $results = $db->prepare($sql);
  $results->execute();
} 
  catch (Exception $e) 
  {
echo "Error!: " . $e->getMessage() . "<br/>";
return false;
}
  return true;
}

2 Answers

Joe Schultz
Joe Schultz
6,191 Points

The problem with this, is that you have a table called podcasts, but you do not know what or how many columns are in the table. So when you do the SELECT command, you have to put in the wildcard * character. That way, you will get all of the data. The answer should look something like this:

try {

    return $db->query('SELECT * FROM podcasts');
    } catch (PDOException $e) {
        echo "Error! " . $e->getMessage() . "</br>";
        return array();
        }   

Hope this helps!

Thanks a lot Joe, it worked

<?php function get_podcasts() { include 'connection.php'; try { return $db->query('SELECT * FROM podcasts'); } catch (PDOException $e) { echo "Error! " . $e->getMessage() . "</br>"; return array(); }
$sql = 'SELECT podcats_id FROM podcasts'; //add code here try{ $results = $db->prepare($sql); $results->execute(); } catch (Exception $e) { echo "Error!: " . $e->getMessage() . "<br/>"; return false; } return true; }