Difference between revisions of "PHP-and-MySQL/C3/MySQL-Part-2/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
\{| border=1
{| border=1

Revision as of 12:20, 11 July 2013

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

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Pratik kamble, Sandhya.np14, Sneha