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 Integrating PHP with Databases Querying the Database with PHP Retrieving the Result Set

Wil Garvey
Wil Garvey
3,258 Points

Connecting and Querying an SQL database

I am trying to create an sql database and link it to my locally hosted website. I have created the database my phpMyAdmin and I have created the code to establish a connection and query. When I run my code on localhost/connections.php the following error is thrown. It looks like my safestreet.sql database is in the correct location in the includes folder.

SQLSTATE[HY000] [14] unable to open database file

This is my connection code:

<?php

$host = "localhost"; $user = "safestreet"; $db = "safestreet"; $pass = "safestreet";

try { $db = new PDO("sqlite:inc/safestreet.sql"); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } catch (Exception $e) { echo $e->getMessage(); exit; }

try { $results = $db->query("SELECT * FROM safestreet"); echo 'Retrieved Results'; } catch (Exception $e) { echo "Unable to retrieve results"; exit; }

?>

Thank you for your time and consideration!

1 Answer

is the db on localhost or is a file like in the videos? BTW if the file is on the same directory you need to add the correct path.

$db = new PDO("sqlite:".DIR."/database.db");

http://php.net/manual/en/language.constants.predefined.php