PHP Basics

Working with External Data Sources

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Working with External Data Sources" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano begins refactoring the application to load data from a database rather than a static PHP file. SQLite is introduced and a DB class is created. The data in the SQLite DB can be viewed with a SQLite viewer extension.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Working with External Data Sources" Lesson

[00:00:00]
>> Maximiliano Firtman: Now, I'm going to make this example or this lab a little bit more complicated, so we're going to change the navigation. I'm going to keep this index.php as it is with a different name, so let's rewrite it as all.php because we are seeing all the objects at the same time, okay?

[00:00:23]
And now we are going to create another index, PHP, if you want, you can copy and paste that one, so let's start with that one. So duplicate the PHP, so keep the old version and the new one under the name PHP, because what I want now we're going to do two things.

[00:00:40]
We are going to try to end up using a database, and also we will define a navigation using what is known as a master detail. Master detail means we are going to start with the list of options, and we will click in one option, and we will go to a details page, that will render me the details.

[00:01:03]
Nothing so complicated, it's just the same data, but just to play with the idea of navigation between pages and how to do that with PHP, what's the design pattern behind this? Let's start with solving the database problem, okay? In the data folder, I also have a JSON version, but it's just for telling you that that's actually simple if you wanna read JSON.

[00:01:28]
So, we can quickly move this to that, so instead of include data.php, what I can do is, I will do it quickly because it doesn't add any value. Because we have already played with JSON, but you can quickly create a JSON variable that you're going to use file, get contents, remember we used that for a URL?

[00:01:51]
Well, now we can use it for a file, so we can take the JSON, I have double parentheses for some reason, I have only one, I need only one, and then for that JSON, what do I need? I need to decode it or encode it, what do you think, encode or decode?

[00:02:11]
>> Student 1: Decode.
>> Student 2: Decode.
>> Student 3: Decode.
>> Maximiliano Firtman: Decode, so I'm going to decode that JSON into exhibits, we are kind of done Yeah, maybe we need to check if you're getting an object or an array or how that works, but it's kind of from now on its kind of the same exercise, okay?

[00:02:29]
So, it's not a big deal, what I'm saying is you don't need to hard code your data in a PHP file, you can use JSON for configuration settings or something. And it's pretty simple to load the JSON and parse it and then using it, does it make sense?

[00:02:48]
I think that the cool part is to use the database, so let's focus on the database part. And for the database, maybe I wanna go back to OOP, so let's create just to separate concerns, let's create the database object. So, following the same pattern that I did before, I'm going to create a folder classes within here, I'm going to create a class DB PHP, P1DV, PHP or database, PHP, we're going to create the class DB, okay?

[00:03:27]
Of course, we need PHP tag, class DB, and by the way, we may have in the future, we can use here even inheritance because we can have an abstract DB, do we have abstract classes here? We do, you know what an abstract class is in Java or C sharp?

[00:03:48]
Can anyone give us a definition?
>> Speaker 4: It's a super class that has functionality that can be inherited.
>> Maximiliano Firtman: So, it's a class that you cannot create options from it, so you cannot instantiate, and to use it, you have to create a subclass that extends from it. And some of the methods might have code, some of the methods might not have code.

[00:04:20]
So, you can have abstract classes, we don't need to create one, but if you want, you can. And then that will have a method, for example, that will say open, we can have a function open, again, you don't need to add code, you can stop there if you want, okay?

[00:04:34]
Because it's an after class, we don't need to get into that, I'm just telling you that that exists. So, and the idea is that I wanna connect to a database, so let's talk a little bit about databases in PHP. PHP, by default, actually has no support for databases, which sounds, come on, because we need to separate between what is known as the standard library and libraries.

[00:05:05]
So, every PHP runtime has the standard library, the standard library is actually the least, by the way, I have a typo here in the opening time. The standard library is everything you see here when you start typing here a, everything you see here that you can see, it's a very long list, just starting with a, just starting with a.

[00:05:29]
So, this is part of the standard library, but then you can enable or disable additional libraries, and some of those libraries are coming from the same team, some are not. And this is something that you set up in your PHP installation. By default, most of the installation have a lot of libraries that are part sometimes of the same group, the same PHP team created those libraries.

[00:05:58]
And for that, we have let's say, providers to connect to the most common databases. So it's not in the standard library, but a kind of, so it's a sibling of the standard library. Anyway, it's always enabled by default on most providers, maybe for performance reasons some cloud provider will tell you, no, we have only these libraries enabled, okay?

[00:06:25]
So which databases can we use? Mostly everything, Postgres, Oracle, Microsoft, SQL Server, MySQL, MariaDB, and so on. They're going to use, because of lack of time, and also because I don't know if you have experience with working with databases or not. It's not a requirement for this course, we're going to use the simplest one that is also enabled by defaulting PHP, that library is SQLite.

[00:06:53]
I'm not sure if you have used it, SQLite an open-source library, that is not targeting PHP, it's actually available for every platform, you can use it right now even on the web client side. So it's available on Android, on iPhone, so if you have a phone with you, the library is included in your phone, it's part of the operating system.

[00:07:18]
It's actually, as it says here, a small, fast, self-contained, high-reliability, full-feature SQL database engine, so first, it's a SQL-based engine, so it's not NoSQL, okay? It's not like Mongo, you're not storing objects, you're creating tables, records on those tables and so on. So, we should just use very simple SQL language, so no worries, but you need to get into databases in case you wanna extend the usage of this.

[00:07:54]
And SQLite is not really a server, which helps us in, we don't need to set up a server and MySQL server or MariaDB server, that might take some time and see if you have permissions and the port and blah. I need to understand how to administrate that MySQL, it's just a file, but we can talk to that file as if it is a MySQL database using the same language, SQL, okay?

[00:08:22]
Any questions on SQLite? I already created the database, and the data is already there, it's this one, Data DB, how can you see the database SQL? If you wanna see the details, if you wanna see what's inside, because it's a binary format, so if I say open anyway, with a text editor, this is what I see, well, I see data there, okay?

[00:08:46]
But yeah, it's a binary file, okay? But I need to find a viewer, there are a lot of viewers out there for SQLite, there are even ones that are just plug-ins for Chrome. So if you say Chrome plug-in for- SQLite, viewer or editor, here are a lot, right?

[00:09:03]
I will take just pick just one.
>> Speaker 4: This extension was in-.
>> Maximiliano Firtman: Yeah, there are a lot, also you have for Firefox as well, I think there is one for Fox, I think it's the best one, some of this one on Scrum, some of them are not so good.

[00:09:18]
Well, let's write this one, it's just the first one I just picked, connect, make contact, this is a website, so SQLite viewer, create a new database. No, I wanna open an existing database that is in PHP, frontend museum, data, data DB, select the table, exhibits, yep that's right and there you can see the data, okay?

[00:09:44]
So, it's the same data, but now inside the database, of course this is compared with the hardcoded array and the JSON this is not read-only you, can update the data as well, okay? So, it's the JSON, now it's just a JSON, actually, you can rewrite the JSON if you want.

[00:10:04]
Okay, so we were going to use that one, okay? That's the goal, so we are going to use that database.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now