Welcome to another php video and today, we’re going to be going over databases. We’re going to be jumping forward and getting straight in. What we want to do is open up your xampp and go to the main directory which is just the local host. It will automatically go to /xampp. And right here, you want to find phpMyAdmin which is right here. It's being rather slow. All right now, since we have phpMyAdmin, let us go ahead and type in your username and password if you have one. Your user name has a “root” by default. And by default, you also have no password. So, if you haven’t told it to give you a password automatically, then your password will just be blank and it will automatically log in for you. But I had it set to ask me for a password so I have to type on it.
And right here, is your phpMyAdmin. It's very basic and simple ways to use it. So, we’re just going to go ahead and jump in to creating your database. And a database is just a storage container for data that you can access with php and many other languages actually. But in our specific case, we’re going over php. So, we’re going to show you how to access it with php. We’re going to call our database—we’ll call it “test_db”. And just leave that a collation unless you know what you're doing and want to change it. We’ll click create and database is being created.
So now, you're inside the database and it has no tables. Now, each database is composed of multiple tables in most instances but sometimes only one. It really depends on your database and what media you’re using it for. So, we’re going to create a table though. And inside our test_db, we’re going to call this table “tempdata”. I’m going to just call it “tempdata”. And number of fields is how many fields it’s going to have. Inside a table, there are certain number of fields that can be accessed from php. And these fields actually store the data like if you wanted to store the number 25, you might store it in test_db. You would store it in the tempdata table. And you could store it in maybe in the field called “integer”. It's really up to you. For our purpose, we’re going to be creating two fields. Click go over here on the side and it loads up.
Now, it's vertical right now but once you get so many of them, I think four so it’ll start going horizontal and you’ll b typing across the field instead of straight up and down. But, when it's vertical, the left field is the first one and it goes to the right. And when it's horizontal, when they go in horizontally, then the top one is obviously the first one. So, for our first field, we’re going to call it “id”. Now, in my opinion, I believe you should always have an id field for everything, every table you use, you need an id field. And basically, this field just keeps track of each row of data that you input into the table. So, just follow along with here. We’re going to do id, we’re going to make it an INT. And the lengths and values, the maximum length it can reach, we’ll put 11. That’s what I usually put. Oh, that’s a really big number. I really doubt that’s going to go over that anytime soon.
The only other thing you really need to change is extra, put it to auto_increment. And you really only do this for the id filled or anything else you want to auto_increment but definitely for the id field. Basically, that means automatically set up the id number depending on the rows of data. So, the first field that we input will automatically get the id one. And you’ll see about that, how that works in a minute. And the last thing you want to do is primary key which is this auto array, this little icon here however it says primary. Check this radio box right by it. And that makes basically sets it as the primary key, as the primary field of data input. So next, the field we’re going to be using, we’ll call it “number”. And we’re also going to store it as an INT. And the Lengths/Values, we’ll make it five. And we don’t even have to change anything else on that. So, go ahead and click the Save button down here. And if you’re feeling confused about databases, don’t be, it may take you a little while to get used to but they’re really simple and you’ll get use to them really fast.
So now, we’re inside tempdata database and we’re on the run the test_db database. And we’re on the tempdata table. So, click that and then we go in here and you can see, here it is. It shows us all the information on the table. You can click brows and view all the entries but you see it says, it's empty because we don’t have any entries in there. Right here is the SQL. Basically, you might want to use that later. That’s just telling you the SQL that was executed on the command you inputted. So since I click browse, it's trying to select all of the data. That’s basically what that’s doing but we’ll get into SQL later.
Now, to add data to the database, you can click the insert key and type in manually or you can do what we’re going to do and use php. So, this tutorial is on php so we’re going to quickly go through and show you how to add a number to the database. So, we’re going to go ahead and go into our xampp folder by typing local host. And then, I store mine in the projects directory. So, I might go ahead and get ready in here. And now, we want to open up notepad. Start a simple php document. And put some files in here. So, databases and php, you first need to connect to the database using mysql_connect which is a php function. And it goes like this, mysql_connect. And then you put the host here which is generally local host. That’s generally for most people, it will bail local host and that’s like we said earlier in the local host, it just goes back to whatever Apache you have running, the local host on it which is also 127.0.0.1.
Next field is your username. If you don’t have one set up, it's always going to be “root”. But if you do have one set up, you put your username there and then password. Now, for moist users, you're just starting out, that will be empty because you don’t have a password. But if you do have a password, then you’ll put your password there. I’ll leave that empty for right now. I'm not really going to type my password on the screen and let you guys see it. But go ahead. And the next step will be to connect to the database. Now, type in mysql_select_db, we’re connected. Now, we need to select the database we want to use. In this case, we’ll be selecting the database. What was it again? Let’s go here, test_db. So, you put test_db. Now, you need to tell it what connection to use. So, we need to give mysql a connection. So, we create a variable. We’ll just call it “c” and we’ll equal it to the value of mysql_connect. So, whenever this function returns, it's going to be implied upon this value of “c”. Then, we can use the value of that variable for the second parameter of mysql_select database. And now, that’s going to select test_db on that connection.
Now, you're connected and you’ve selected the database. You want to be able to place some values in it. So, we’re going to—this is actually where SQL comes in. Okay, so, we’re going to be going over basic SQL today. To execute an SQL query—
Transcription by:
Scribe4you Transcription Services