PHP-and-MySQL/C3/MySQL-Part-2/English

From Script | Spoken-Tutorial
Revision as of 12:13, 29 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
0:0 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.
0:14 Now we will insert some dummy data inside our database by doing this.
0:21 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.
0:33 You can see that just popped up.
0:35 And we can also enter our gender here.
0:37 As this is mysql php tutorial, I'll show you how to insert data using mysql or php.
0:49 Now first of all we need to connect to our database.
0:52 Under "mysql dot php" file, we will use the "include" function to include our file "connect dot php".
1:00 Now if this is not in the same directory, you know that you can say "sub directory and then connect".
1:07 Please specify this properly.
1:09 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.
1:18 The "require" function kills the page if this isn't found from here on.
1:23 The "include" will include it and then it will continue to echo or continue to run the rest of the page.
1:29 If you use the "require" function, it will in fact "kill" if this cannot be included.
1:34 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.
1:41 We will get loads of rubbish upon the page.
1:44 ok... so if "require connect dot php" and inside connect dot php we need to start our php mysql functions.
1:52 Firstly you need to know - we will start with our variable called "connect" and this will use the function "mysql_connect".
2:01 That's the first function you need to learn.
2:03 Its a very very important function that enables you to connect to your database mysql.
2:08 This takes 3 parameters.
2:11 The first one here is the webserver itself - the address of the webserver.
2:17 I'll use my computer at the moment with a local webserver with my local host.
2:22 This can also be written as 127.0.0.1 if you wish, as a substitute for local host.
2:32 I prefer typing "local host" personally.
2:35 Now I'll use the standard username and password given to me.
2:41 This is "root".
2:42 My password doesn't exist because I don't have a password.
2:50 We have established our connection but what happens if this connection is not initialized properly.
2:56 What we can do after this, is write "or die" and in brackets we can specify an error message for example "connection failed".
3:02 So lets presume at the moment this connection works.
3:11 I'll echo out a piece of code saying "connected".
3:18 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.
3:26 So what I will do is, I will open backup here.
3:30 Refresh and you can see "connect dot php" and "mysql dot php" and I'll click on mysql dot php.
3:37 The reason I'm not clicking connect is because inside mysql we require "connect dot php" anyway.
3:44 So as long as these are both saved, we can just run mysql dot php.
3:48 We have connected successfully.
3:50 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.
4:08 I can refresh and .... its taking a long time...... ok there we are.
4:14 You can see that we have got mysql error here and we have connection failed text just here, that we specified earlier.
4:21 Ok.. so we have got unknown mysql server host.
4:25 If you ever get this error you know what to look out for.
4:27 This is the host I specified and you can see what line its on and all our usual debugging message code.
4:36 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.
4:46 This is the second function that you should learn.
4:50 Its "mysql error" - just put brackets like that - and when we refresh our page keeping the "I don't exist".
4:57 We can refresh and its taking its time.....
5:06 Ok there we are.
5:07 What we have basically done is we echoed out the same error message that php has given us anyway.
5:12 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.
5:24 Now we don't echo out this to the user.
5:26 lets say we go up here and say "error reporting".
5:30 Please see the error reporting tutorial I made, if you haven't.
5:33 If you have...ummm.... set this to '0'.
5:40 This will turn off all error reporting.
5:43 So what happens is this error here will be ignored but our specific error will be given out to the user.
5:49 Lets refresh here.... again its taking its time...so I apologize....
5:58 There you go. We can just say that we got our specific error out there, ok?
6:03 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
6:13 To do this, we use the "mysql_select db" function.
6:20 This takes exactly 1 parameter and that's the name of your database.
6:24 So we click on "php myadmin" again, we can see that our database name is "phpacademy".
6:31 So if I just type "phpacademy" this should work.
6:36 Again we could use this or die feature.
6:40 Using our die function we can specify a mysql_error if it doesn't exist or anything like that.
6:47 So lets refresh this.
6:50 In fact I'll change this back to "local host" because I'm getting back on track here and then lets refresh.
6:59 So that's connected and if this is not found we are giving out a mysql_error.
7:04 Lets try that - "I don't exist" and refresh and "Unknown database "idon'texist"".
7:12 This is working well.
7:14 Its quite useful to have these errors and then we can get users to report back if they don't exist.
7:20 So its "phpacademy" there.
7:23 I presume everything is ok and lets refresh.
7:29 Change this back to "phpacademy" and save that.
7:33 Refresh and we have been connected successfully.
7:36 What I'll do is keep a log of this and say that I am connected successfully.
7:41 I'll end our paragraph after this to continue with the rest of our code.
7:42 Next thing is to write some data to our database which we will cover in our next tutorial.
7:56 See you soon! This is Juanita Jayakar dubbing for the Spoken Tutorial Project.

Contributors and Content Editors

Chandrika