Getting Started with PDO4:13 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".
You need to sign up for Treehouse in order to download course files.Sign up