Hi everyone!
This is a pure My SQL php tutorial.
I will teach you the basics of connecting, retrieving data, handling errors and modifying data.
So, that will cover some SQL code and some SQL queries.
Ok! Lets start.
Here I am going to show the directory structure of "mysql"
and we will create a couple of files here.
I will create the first file and call it "connect.php".
I will come here, click on the folder called "mysql" and Save this as "connect.php",
Now here we will create a separate file and include with every page that we use.
This is a lot easier to connect to your database,
What we will do is type our "include" function and specify this file.
I will create another file which is my main "mysql" file
with a code that I will show you all.
Ok, so I have got my mysql dot php open.
This is php codes and we need the php tags and the same will connect to php.
I will explain this "include" function in a minute.
First, I will teach you how to connect to the database.
If you don't know where you have it stored on your web server, then I suggest you check out an application called phpmyadmin.
It is a database interface php written program or in other words a script.
Here we will look at the inside of my database, my service.
Rather my server, My SQL server. It gives our table information, our database information and information about my server etc etc.
Although, we don't need to know about it, this is a good start to a program, if you are just starting to use a php mysql or just mysql in general.
It is a good way to start interfacing with your database, instead of using the command line to do things.
Using command line could be difficult for the first timers.
Ok, so what we see here are, our databases.
I have got one called "phpacademy" and I have got one called "phplogin" which I mentioned in another tutorial that I have created.
The others are just a standard.
They are just for holding data.
Don't delete them.
All you need to do is create new databases.
To do this, you have got a simple box here.
We are just going to create databases.
I will work within my php academy database for now.
It is a lot easier.
So its creating a database now.
It is simple.
You type in the name and click on "Create".
My php has already been created here.
So I will use this one.
Click on it and you can see that there are several tables inside.
This is denoted by the symbol here when using phpmyadmin.
This is guestbook from my guestbook tutorial.
Now I will create a new table on this database for the specifics of this tutorial and I shall call it "people".
The number of fields is quite important.
You can't leave this blank.
The number of fields on your table is for storing each column of data.
For example, the first one usually when you are dealing with records, you can have an ID which is a numerical value.
So it is a number that is going to increment each time.
It will allow you to reference your records being stored individually by the unique number.
And usually set this to the primary key.
If you are not familiar with the databases, you need to start looking out for terms like primary key.
I will not be dealing with secondary keys because its quite flexible way to do this anyway with mysql database.
Just read up on databases in general, if you have Microsoft access or any other database .
I suggest you to learn about groups of database.
Ok, so the number of fields depends on how much data you want to store and what data you want to store.
Usually when I am creating fields, I will bring out an usual blank document.
And I will start by typing out the fields that I want.
The first one is always going be ID.
This is a self incrementing value every time I create a new record.
So it will be 1 for the first record, 2,3,4 and the data will be stored after this.
It is a very useful field.
Since my table is called "people", I will store some data about people.
So I will first type in the firstname and then I will type in the lastname and then the age and the gender.
We can leave it there for now, to keep it simple.
So here we can see that we have got 5 fields.
I shall go back here and type in 5 and click "Go".
It will also be great to see a pop up here in a minute.
No you won't because we haven't created our field names yet.
Ok! We have a standard here.
There are a lot of options for these.
But, a field is a fieldname.
So the first one is going to be "ID".
The type is the data type, that you want to store this field in.
Anything that goes into it must be added here to this datatype.
"varchar" which stands for variable characters is quite common. It is a very useful one and it requires a length.
We can have 25 characters long here.
50 characters long.
Or 100 characters long.
Or 1 character long.
Actually we are just storing the type and the length of the data stored.
It helps in storing, for example your firstname.
Lets say our fieldname here is "firstname" and I have a "varchar".
There is no point in typing in 500 characters because we will be using unnecessary amount of data.
A typical firstname will be no more than 25 characters.
Even if it is, it will not be more than 30 or 35 characters.
But for now I will store my "firstname" as 20 to 25 characters and I will put 20 here.
Our "ID" will be an integer because it is a number.
It will be self incrementing.
This will be 1,2,3,4
The amount of records we use.
And we have some other options here.
That is here is primary key.
We are going to select it and at extra, we can see that we got "auto underscore increment".
This is an auto increment.
It will give this particular function.
Whenever you enter a new record, this will also automatically go up.
So here we have "firstname".
We have "lastname" and again I will set this as 30.
And what else do we have???
We have "age" and obviously this is an integer and we have "gender".
Ok.
Now here instead of "age", I will say say "Date of birth".
So this is Date of Birth.
I will set this as date.
Here I am trying to find a date datatype and I will see how this works.
So our length for date doesn't have to be set here.
We have a standard format for this. So we don't have to worry about that.
Now I will set the "gender" as "varchar" of character 1.
Now we can store "M" for male and "F" for female.
Ok. If we go across here, we can see that there are a lots of options.
You can comment this yourself.
You can remind yourself what this field does.
But usually name your fieldname appropriately so you know what data you are saving.
Ok. Here I will click on "Save" and you can see that "people" has appeared here.
This will ask you a query here.
Now when I was talking about command line earlier, this is what you have to type to create that.
However, we have used a graphic user interface to save ours.
We can see down here, we have our fields, our types and our collation attributes, null data for example.
The default value that is stored as.... say for example if you had a field saying "Has the user registered?"
Or anything of your choice. You could use the default here too.
For example if I wanted to store everyone, I have registered here as male by default or female by default, I could type "M" or "F" here.
And we have auto increment here and also some other data that we don't need to know about in this tutorial.
Ok, here we have created our table and if you go in part II of this, I will show you how to insert some data and also how to retrieve this data from your database using php.
Join me in part 2. This is Evan Varkey dubbing for the Spoken Tutorial Project. (Script contributed by Juanita Jayakar).