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 Environment Variables with PHP Environment Variables Complex Data Types

Shoko Ishigaki
Shoko Ishigaki
21,826 Points

Database source file for PDO constructor

I noticed that "sqlite:/www/" was used to construct PDO object in the video and also in the teachers note.

$db = new PDO("sqlite:/www/" . $repository->source);

However, I had to remove "/www/" to make the code work (I'm using localhost).

$db = new PDO("sqlite:" . $repository->source);

My question is what is the meaning of "/www/" and also when to use it.

jamesjones21
jamesjones21
9,260 Points

www seems to be a folder directory, do you have the full code? I know a little about PDO, but not used SQLite before.

but hopefully I can help

Shoko Ishigaki
Shoko Ishigaki
21,826 Points

Here's the full code.

<?php
putenv('REPOSITORY={"type":"sqlite","source":"inc/quotes.db"}');
include 'inc/bootstrap.php';
$pageTitle = "Developers Say the Darndest Things";
$section = "home";
require 'inc/header.php';
?>
    <div class="wrapper">

        <?php
        // get each line of file into an array
        // $file = file('inc/quotes.txt');
        //read environment variable into an object
        $repository = json_decode(getenv('REPOSITORY'));
        //echo $repository->type;

        switch ($repository->type) {
            case 'text':
                $file = file($repository->source);
                // grab a random array element
                $quote = $file[array_rand($file)];
                break;
            case 'sqlite':
                try {
                    $db = new PDO("sqlite:" . $repository->source);
                    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                    $result = $db->query('SELECT quote FROM Quotes ORDER BY RANDOM() LIMIT 1');
                    $quote = $result->fetchColumn();
                    break;
                } catch (Exception $e) {
                    echo "Error!: " . $e->getMessage();
                }
            default:
                $quote = 'It works on my machine.';
        }

        echo '<h1>' . $quote . '</h1>';
        ?>
    </div>
<?php
require 'inc/footer.php';

1 Answer

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

/www/ is the base path for workspaces, so if you are working locally you wouldn't need that :)