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

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

) after this to continue with the rest of our code.

07:53 Next thing is to write some data to our database which we will cover in our next tutorial.
08: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