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 trialLucas Santos
19,315 PointsBoth PHP Treehouse teachers Randy and Hampton use different methods on connecting to the database. Please explain
I do not fully understand why Hampton and Randy use different methods on connecting to the database through PDO. Can someone please elborate on what the difference is and which one is better.
Also one other thing is on Hampton's method using sqlite he just refers the connection to database.db but nothing is within that file. What is suppose to be in there to establish connection?
Hampton's Method:
$db = new PDO('sqlite:./database.db');
Randy's Method:
$db = new PDO("mysql:host=$servername; dbname=$database; port=$port", $username, $password);
I would really appreciate if someone elaborate, thank you.
2 Answers
thomascawthorn
22,986 PointsYeah it's super simple :-)
PDO stands for php data object - imagine it as a runner between your application and a database. It's a 'thing that gets and sets data'.
It supports quite a few different types of database as you can see - but remember it's job is only how to fetch and get data - it doesn't care about which database type you choose. The link gives you a list of drivers. These drivers are the middle men between PDO and databases. Say you're using MySQL, PDO will say "I want this data!" and it will chat to the MySQL Driver. The driver will know exactly how to connect and chat to a MySQL database and send PDO all the information it asked for.
Sqlite is often used for quick demos because it's a lot quicker to setup than MySQL - you don't have to worry about connections, users and ports so much. Just jump into a file and off you go! Because it's so quick, you'll see it used for a lot of application tutorials around the web where the database is not the focus of the lesson. MySQL is a more 'heavy duty' database that you'll want to use for your bigger projects.
Lucas Santos
19,315 PointsVery well said. Thank you for helping me understand, appreciate it!
Emmanuel Salom
Courses Plus Student 8,320 PointsThey are both using the same method. They are connecting to the db using mysql PDO. Randy is including a set of attributes. You can use attributes depending on your SQL set up.
They are using 2 different databases mysql and sqlite.
You can choose the one you like to work with. They both work with pdo.
You should read php mysql pdo documentation to have a better understanding. Of how this works.
Lucas Santos
19,315 PointsI see, cool thanks.
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsHI
, I dontknow but the first look as SQLITE? mabe that's different since Randy is in MySQL I don't know guess ing. BUt what I can say is that if you give 10 programmers same task, thye do it in 10 different ways. So I think it might be this.