Connecting to the Database4:00 with Alena Holligan
The PHP Data Object (PDO) allows PHP to interact with a database. This project uses a SQLite database, but you can connect to many different types of databases using a PDO. This video will show you how to find and use the documentation for PDO so you can then use that documentation to connect to other databases as well.
PHP provides a large number of predefined constants to any script which it runs. More information on these Magic Constants can be found here.
Connecting PDO to MySQL
$db = new PDO("mysql:host=localhost;dbname=DATABASE_NAME;port=3306","USERNAME","PASSWORD");
For MySQL, you specify mysql and a colon, then type "host=" followed by the name of the host to which you want to connect. After that, you type a semicolon, followed by dbname and an equal sign, followed by the name of the database. If your environment is using the default MySQL port of 3306, this string here might be all you need.
MAMP uses a different port (8889 by default), so if you haven't changed the default, you will have to specify the port number. You type another semicolon, followed by the word "port" and an equal sign, followed by the port number.
That's the first argument to pass in when instantiating a new object from the PDO class. It's all surrounded by 1 set of quotation marks because it's 1 PHP string.
The word “host”, here, is not a PHP variable— it's just part of a piece of text that gets used to identify the database.
For MySQL, the second argument is the username and the third argument is the password. By default, the MySQL username and password on MAMP are both "root".
The shell of the project is all set up for you including CSS styles and forms. 0:00 There's also a SQLite database with all the tables and 0:05 fields that you need as well as some of the initial test data. 0:08 In this video we use PHPs data object or PDO for 0:11 short, to connect the application to the database. 0:15 For accessing the projects and test data. 0:18 You should already be familiar with setting up a connection to the database. 0:20 But we'll cover everything that you need to know in this video. 0:24 If you'd like more details than what's covered here, 0:28 check out the notes associated with this video. 0:30 We're going to write code to instantiate or 0:33 create an instance of an object from the PDO class. 0:35 This object variable gives us all the attributes and 0:39 actions that we need to interact with the database. 0:42 Let's go into workspaces. 0:45 Let's create a new file for connecting to the database. 0:48 Let's add this file to the includes directory, inc for short. 0:51 We'll name this file connection.php. 0:55 To create an object from a class, you first specify a variable name for 1:00 the object you want to create. 1:04 We'll call our object $db for database. 1:06 We want to assign this variable a value. 1:10 So we're using equal sign. 1:12 To create a new object, we use the keyword new. 1:15 We then specify the class name followed by parenthesis. 1:19 This creates a new empty object variable of the type PDO 1:24 just like calling the function array creates an empty array. 1:29 Also like the array function, 1:33 you can often pass arguments when you create a new object. 1:35 With a PDO object, 1:38 you pass in the information needed to connect to the database. 1:40 We'll be using SQLite for the following example. 1:44 So let's check out the documentation to remind us about what is needed for 1:47 that database. 1:51 In your browser search for pdo sqlite. 1:52 The first result is the one we want. 1:56 PDO SQLite is a driver that implements the PHP data object 2:00 to enable access to SQLite 3 databases. 2:05 A driver is a piece of software that connects one thing to another. 2:08 In this case, the PDO driver connects SQLite to PHP using the PHP data object. 2:12 In the Table of contents the first link is for PDO SQLLite DSN. 2:20 in order for 2:26 PDO to use the SQLLite database, we need to pass the data source name. 2:28 To access a database on disk, 2:34 we pass the prefix SQLite followed by the absolute path to the database. 2:36 Okay, let's go back and add this to our connection. 2:41 Within our call to the PDO class we're going to add sqlite;. 2:44 And in our case the database is in the include folder as well. 2:51 So we're going to use the magic constant 2:55 __DIR__ and then /database.db. 3:01 On the next line, we're going to var dump our db variable. 3:07 This will make sure that we've created our new object. 3:13 Let's run this file in the console to see what's going on. 3:16 View > Show Console. 3:19 Type php inc/connection.php. 3:22 We see that it's dumping an object. 3:29 It's a PDO object. 3:31 So our PHP code is now connecting to an external system, a SQLite database. 3:33 But what if we make a mistake, like a spelling error. 3:39 Let's change SQLite to have two Ls since that's the way I wanna type it anyway. 3:42 Now we get a bunch of errors and we don't want that. 3:52 So the next thing for us to do is a little bit of error handling. 3:56
You need to sign up for Treehouse in order to download course files.Sign up