Hi. In the first part of the tutorial we created a table inside our database "php academy" and we created our fields with all the relevant data to go with it... datatypes etc.
Now we will insert some dummy data inside our database by doing this.
I'll not click this "Insert" button here because by clicking this button here, we get very easy to use interface where we can type in firstname, lastname, date of birth using the calender function over here.
You can see that just popped up.
And we can also enter our gender here.
As this is mysql php tutorial, I'll show you how to insert data using mysql or php.
Now first of all we need to connect to our database.
Under "mysql dot php" file, we will use the "include" function to include our file "connect dot php".
Now if this is not in the same directory, you know that you can say "sub directory and then connect".
Please specify this properly.
If you dont want the page to execute... if you don't want the "Rest of the page" to execute here, you can use the "require" function.
The "require" function kills the page if this isn't found from here on.
The "include" will include it and then it will continue to echo or continue to run the rest of the page.
If you use the "require" function, it will in fact "kill" if this cannot be included.
So what I will say is "require connect dot php" just to say that if you can't connect to the database, the rest of this page is meaningless.
We will get loads of rubbish upon the page.
ok... so if "require connect dot php" and inside connect dot php we need to start our php mysql functions.
Firstly you need to know - we will start with our variable called "connect" and this will use the function "mysql_connect".
That's the first function you need to learn.
Its a very very important function that enables you to connect to your database mysql.
This takes 3 parameters.
The first one here is the webserver itself - the address of the webserver.
I'll use my computer at the moment with a local webserver with my local host.
This can be written as 127.0.0.1 if you wish, as a substitute for local host.
I prefer typing "local host" personally.
But I'll use the standard username and password given to me.
This is "root".
My password doesn't exist because I don't have a password.
We established a connection but what happens if this connection is not initialized properly.
What we can do after this, is write "or die" and in brackets we can specify an error message for example "connection failed".
So lets presume at the moment this connection works.
I'll echo out a piece of code saying "connected".
Ok now if this does connect successfully the rest of the script will run and echo out "connected" otherwise it would just give you this text and won't run the rest of the page.
So what I will do is, I will open backup here.
Refresh and you can see "connect dot php" and "mysql dot php" and I'll click on mysql dot php.
The reason I'm not clicking connect is because inside mysql we require "connect dot php" anyway.
So as long as these are both saved, we can just run mysql dot php.
We have connected successfully.
Now if I were to say change this to something like "I dont exist" then we get a connection error because that host name doesn't exist, .... at least on this computer.
I can refresh and .... its taking a long time...... ok there we are.
You can see that we have got mysql error here and we have connection failed text just here, that we specified earlier.
Ok.. so we have got unknown mysql server host.
If you ever get this error you know what to look out for.
This is the host I specified and you can see what line its on and all our usual debugging message code.
So lets presume that I...ummm.... in fact what I can do first is show you another useful thing or "die" you can also specify another function here.
This is the second function that you should learn.
Its "mysql error" - just put brackets like that - and when we refresh our page keeping the "I don't exist".
We can refresh and its taking its time.....
Ok there we are.
What we have basically done is we echoed out the same error message that php has given us anyway.
However if your.. ummm.... how do I say - if your error reporting is turned off for your user, this will give out what you want.
Now we don't echo out this to the user.
lets say we go up here and say "error reporting".
Please see the error reporting tutorial I made, if you haven't.
If you have...ummm.... set this to '0'.
This will turn off all error reporting.
So what happens is this error here will be ignored but our specific error will be given out to the user.
Lets refresh here.... again its taking its time...so I apologize....
There you go. We can just say that we got our specific error out there, ok?
Presuming that we have connected successfully using this function and if not, then we have given out this error message, the next thing to do is to select our database
To do this, we use the "mysql_select db" function.
This takes exactly 1 parameter and that's the name of your database.
So we click on "php myadmin" again, we can see that our database name is "phpacademy".
So if I just type "phpacademy" this should work.
Again we could use this or die feature.
Using our die function we can specify a mysql_error if it doesn't exist or anything like that.
So lets refresh this.
In fact I'll change this back to "local host" because I'm getting back on track here and then lets refresh.
So that's connected and if this is not found we are giving out a mysql_error.
Lets try that - "I don't exist" and refresh and "Unknown database "idon'texist"".
This is working well.
Its quite useful to have these errors and then we can get users to report back if they don't exist.
So its "phpacademy" there.
I presume everything is ok and lets refresh.
Change this back to "phpacademy" and save that.
Refresh and we have been connected successfully.
What I'll do is keep a log of this and say that I am connected successfully.
I'll end our paragraph after this to continue with the rest of our code.
Next thing is to write some data to our database which we will cover in our next tutorial.
See you soon! This is Juanita Jayakar dubbing for the Spoken Tutorial Project.