How to Speak Database

Video loading...

  • 0:16
    PHP for Beginners Part 3
  • 0:18
    How to Speak Database
  • 0:20
    with Leanna Pelham
  • 0:27
    Hey there.
  • 0:28
    Things are about to get crazy, because it's
  • 0:31
    time to learn all about databases.
  • 0:34
    We already have a data source that makes our application dynamic.
  • 0:37
    It reads and displays pet data, which
  • 0:40
    happens to be stored in a file called pets.json.
  • 0:43
    If we change something in this file, the site updates automatically.
  • 0:47
    In a fully built site you'll need to read and write
  • 0:49
    lots of data, like a user's profile, comments, purchases, or maybe
  • 0:54
    forum posts so people can complain about movies.
  • 0:58
    And yeah, we could build that site by entirely reading data
  • 1:01
    from flat files, like users.json and angrymovieforumpost.json.
  • 1:08
    A database is a place to put data, just like these files
  • 1:12
    but, instead, we store things in tables.
  • 1:14
    And we won't be using file_get_contents
  • 1:16
    and file_put_contents to read and write data.
  • 1:19
    We'll use queries, which are kind of like human sentences
  • 1:23
    that describe the data you want.
  • 1:25
    Yeah, but what is this database thing?
  • 1:29
    So first, a database is a piece of software you run on your computer,
  • 1:34
    just like a web server like Apache.
  • 1:36
    People can talk to the web server by making a request to our machine,
  • 1:40
    usually to port 80.
  • 1:42
    Actually, in our tutorial we've been using port 8000.
  • 1:46
    A port is like a door and your web server
  • 1:48
    is watching for requests to port 80 so it
  • 1:50
    can process it and return a page.
  • 1:52
    To talk to a database, we send a query to our computer.
  • 1:56
    Usually to port 3306.
  • 1:59
    If the database software is running, it
  • 2:01
    watches for queries coming to this door,
  • 2:03
    interprets them, then sends back the data we're asking for.
  • 2:07
    Yes we are going to actually do this, but let's focus on the fact
  • 2:11
    that a database is just a standalone piece of software
  • 2:14
    that we talk with to get data.
  • 2:17
    To make requests to a web server, we typically use a browser.
  • 2:20
    To send a query to a database we have a few options,
  • 2:24
    but the most basic is to use a command line program called MySQL.
  • 2:29
    MySQL is the most common database software
  • 2:33
    and was installed for you when you installed XAMPP in episode one.
  • 2:37
    Let's open up the command line.
  • 2:39
    In Mac, I can just type terminal into Spotlight to find it.
  • 2:43
    Oh, and command line and terminal mean exactly the same thing.
  • 2:48
    If you're using Windows, we used a terminal
  • 2:50
    inside XAMPP's Control Panel in episode one.
  • 2:53
    I recommend using that or downloading get, which comes
  • 2:56
    with a nice Git Bash terminal program.
  • 2:59
    Windows does have a built in terminal called CMD,
  • 3:02
    but it's really light on features.
  • 3:05
    We're going to ease into using the command line, so don't worry.
  • 3:09
    You probably can avoid using the terminal,
  • 3:11
    but you'll be a much better developer if you and the terminal
  • 3:14
    become friendly.
  • 3:16
    Ready?
  • 3:17
    Type mysql --help.
  • 3:20
    Ugh!
  • 3:21
    MySQL's help information is a bit chatty,
  • 3:24
    but hey, things are working!
  • 3:27
    This MySQL program is like a browser to the database.
  • 3:31
    No, it's not the database, just like your browser is not the website.
  • 3:36
    It just helps us talk to the database,
  • 3:38
    which might live on some other server.
  • 3:40
    If we give it the IP address of the server
  • 3:42
    where the database software is running,
  • 3:44
    it'll let us write queries and send those to it.
  • 3:48
    Instead of typing this into the address bar, we do it like this.
  • 3:51
    This says, I want to talk to a database located at the IP address
  • 3:55
    local host and go through port 3306.
  • 3:59
    You should log into that database using the user route
  • 4:02
    and you should ask me what the password is.
  • 4:05
    When we hit Enter, it asks us for a password.
  • 4:08
    XAMPP gives the root user a blank password, so just hit Enter.
  • 4:12
    If that doesn't work, try route.
  • 4:15
    We're in!
  • 4:17
    Our login information was just sent to local host, which
  • 4:20
    is that special word that points right back to our own machine.
  • 4:24
    It knocked on port 3306.
  • 4:27
    Since XAMPP installed the MySQL database software
  • 4:29
    and configured it to look on this port, MySQL received our details,
  • 4:34
    checked the username and password, and basically said, welcome!
  • 4:37
    Come on in!
Loading ...

How to Speak Database

Loading...

We already have a data source from PHP for Beginners Part 1 and PHP for Beginners Part 2 that makes our application dynamic. It reads and displays pet data, which happens to be stored in a file called pets.json. If we change something in this file, the site updates automatically. For a more complex site, however, we'll need to read and write a lot of data. So we're going to dive into the world of databases now and learn how to manage more data. This first tutorial covers database basics and how to connect and communicate with a MySQL database.

Downloads: 
Log in or sign up to download companion files.
Additional resources: