WEBVTT 00:00:08.880 --> 00:00:13.710 In this video we get to learn how to retrieve data from a database. 00:00:13.710 --> 00:00:19.050 So first of all we're going to need to connect to our database so we're going to be using the same code 00:00:19.500 --> 00:00:24.960 we used earlier to connect to a database so you can just copy from the previous file. 00:00:25.390 --> 00:00:26.270 OK. 00:00:27.000 --> 00:00:35.090 And then we are going to write some code to retrieve data from a table. 00:00:35.100 --> 00:00:36.380 OK. 00:00:37.020 --> 00:00:43.430 So let's have a look at our table in the place might add me. 00:00:43.680 --> 00:00:50.310 So in order to get all the rows of the table just like this we have run this query. 00:00:51.120 --> 00:00:56.300 And if you read the query says select start from users where 1. 00:00:56.560 --> 00:00:57.230 OK. 00:00:57.350 --> 00:00:59.780 And that's the equivalent of select stuff from users. 00:00:59.810 --> 00:01:02.730 OK. 00:01:03.030 --> 00:01:06.040 Because one is true. 00:01:06.060 --> 00:01:07.020 OK. 00:01:07.140 --> 00:01:18.630 And then we use a key word limit starting with 0 and then Terzi So 0 is the row where we're going to 00:01:18.630 --> 00:01:19.220 start. 00:01:19.500 --> 00:01:23.440 And 30 is the number of rows that you would like to show. 00:01:23.970 --> 00:01:28.570 So this means that we're going to show so as heroes starting from record 0. 00:01:29.280 --> 00:01:30.750 OK. 00:01:31.830 --> 00:01:42.120 OK so now we are going to create a query that we're going to store in a variable as curial. 00:01:42.330 --> 00:01:46.110 So these queries just going to select all the rows of our table. 00:01:46.130 --> 00:01:53.450 So we're going to write selects staw from us. 00:01:54.030 --> 00:01:54.820 OK. 00:01:54.900 --> 00:01:55.350 You can. 00:01:55.350 --> 00:01:59.530 Right where one but it does the equivalent of this anyway. 00:02:00.180 --> 00:02:04.900 So now we are going to execute our query. 00:02:05.170 --> 00:02:05.950 Okay. 00:02:06.030 --> 00:02:12.190 And the results of the execution function are going to be stored in a variable. 00:02:12.330 --> 00:02:14.940 But we're going to call results. 00:02:16.040 --> 00:02:16.680 OK. 00:02:16.740 --> 00:02:25.140 So inciter is also going to store the outputs of the function my school-I query. 00:02:25.950 --> 00:02:32.190 And if you remember this function will take a couple of parameters so the first one is the connection 00:02:32.190 --> 00:02:39.540 variable and the second parameter is the query again. 00:02:42.240 --> 00:02:48.270 So to check if this is successful we are going to place this inside the if condition. 00:02:48.530 --> 00:02:50.570 OK. 00:02:50.740 --> 00:02:59.800 And we're going to write if results equals this then we're going to execute some code. 00:03:00.110 --> 00:03:01.400 OK. 00:03:01.570 --> 00:03:10.470 And to start we are just going to print these results very well using the prince and his function. 00:03:11.550 --> 00:03:13.340 OK. 00:03:14.040 --> 00:03:25.380 And if we save and try lots we get here an objects which means that this has been successful and we 00:03:25.380 --> 00:03:31.530 have saw the results of this function inside the results variable and the result. 00:03:31.530 --> 00:03:34.080 We know that if it's successful it's an object. 00:03:34.540 --> 00:03:37.000 OK. 00:03:37.110 --> 00:03:45.090 All right so now how do we get the table rose. 00:03:45.090 --> 00:03:53.420 OK so first of all we are going to check the number of rows corresponding to our results. 00:03:53.460 --> 00:03:54.150 To do that 00:03:57.840 --> 00:04:00.810 we're going to use the function my as Kulai 00:04:04.050 --> 00:04:07.700 underscore number call rows. 00:04:08.260 --> 00:04:11.280 OK. 00:04:11.500 --> 00:04:16.560 And it's going to take as a barometer the results variable. 00:04:17.150 --> 00:04:20.390 OK. 00:04:20.400 --> 00:04:25.570 So let's just echo this. 00:04:27.050 --> 00:04:35.570 Now I can see that you got five rows and I see exactly the number of rows in our table. 00:04:35.880 --> 00:04:36.760 OK. 00:04:37.200 --> 00:04:38.850 So what are we going to do. 00:04:38.850 --> 00:04:46.630 We are going to check if the number of rows is greater than zero. 00:04:48.120 --> 00:04:54.550 OK then we are going to execute some other code. 00:05:01.760 --> 00:05:02.960 All right 00:05:05.870 --> 00:05:11.460 now we are going to cycle through all the arrows of our results. 00:05:11.810 --> 00:05:12.630 OK. 00:05:12.640 --> 00:05:18.670 Using another function that we call my school-I fetch array. 00:05:19.150 --> 00:05:23.710 So this function is going to take as a barometer our results sets. 00:05:23.710 --> 00:05:27.050 So in our case it's going to be our results very well. 00:05:27.250 --> 00:05:28.480 Okay. 00:05:28.630 --> 00:05:33.910 And every time we call this function it is going to fetch the next row. 00:05:34.220 --> 00:05:34.970 Okay. 00:05:35.060 --> 00:05:41.150 And then he's going to return an array of strings corresponding to that throw. 00:05:41.170 --> 00:05:45.240 So if we call it the first time it's going to go to row number one. 00:05:45.440 --> 00:05:54.050 Fetch it and then he's going to return and the array corresponding to that row and the array could be 00:05:54.340 --> 00:05:57.270 associated numeric or both. 00:05:57.280 --> 00:06:03.340 And then when we call it the second time it's going to go to the second row return an array and so on 00:06:03.340 --> 00:06:10.390 until we have no more rows in the result sets and in that case it is going to return not. 00:06:10.900 --> 00:06:11.790 OK. 00:06:11.830 --> 00:06:17.140 So let's try to call this function several times. 00:06:17.590 --> 00:06:25.250 So in our case we of course a table of five rows in our result sets should have five rows in it. 00:06:25.420 --> 00:06:28.930 So let's call our function six times. 00:06:28.940 --> 00:06:33.970 So the five first timers we're expecting the function to return an array of strings. 00:06:34.190 --> 00:06:38.750 But the six thousand six time we are expecting the function to return. 00:06:38.770 --> 00:06:39.710 No. 00:06:40.270 --> 00:06:42.850 OK so let's call the function 00:06:46.880 --> 00:06:52.500 six times so let's give its results viable as an argument. 00:06:52.720 --> 00:07:01.810 OK and let's bring the results using DePrince underscore our function. 00:07:04.260 --> 00:07:06.110 Okay. 00:07:06.870 --> 00:07:11.990 Just before that let's echo a paragraph saying Call number 1 00:07:16.810 --> 00:07:17.080 K 00:07:21.300 --> 00:07:27.010 and is capitis several times. 00:07:27.290 --> 00:07:35.930 That's going to be called them 2 3 4 6 4 3 5 and 6. 00:07:35.960 --> 00:07:39.720 Ok so if we save and refresh. 00:07:40.240 --> 00:07:40.900 Okay. 00:07:40.930 --> 00:07:44.390 So you can see that call number 1 gave us an array. 00:07:44.780 --> 00:07:45.580 Okay. 00:07:45.640 --> 00:07:53.530 So just one thing before we look at the AS which row we are looking at you can see here those for the 00:07:53.530 --> 00:07:56.480 same value we have two elements. 00:07:57.110 --> 00:07:57.610 Okay. 00:07:57.820 --> 00:08:01.900 And then we have the same thing same value twice. 00:08:01.960 --> 00:08:09.250 So that first the first one corresponds to the numeric key and then the second one corresponds to an 00:08:09.290 --> 00:08:10.200 agitated key. 00:08:10.290 --> 00:08:10.900 Okay. 00:08:11.320 --> 00:08:20.140 So we are having here a combination of switches and numeric array but if we want to have only associative 00:08:20.870 --> 00:08:28.610 array then we can add another barometer to our my is Karoli fate array function. 00:08:28.950 --> 00:08:29.660 Okay. 00:08:29.680 --> 00:08:43.210 And that parameter is going to be my school-I my Escurial I underscore associative. 00:08:43.830 --> 00:08:46.530 OK all in capital letters just like these. 00:08:46.580 --> 00:08:53.260 So if we save that and refresh our page then we are getting a raise. 00:08:53.290 --> 00:08:59.120 OK if we want numeric arrays then we just replace does by numeric. 00:08:59.610 --> 00:09:02.720 And this is what we get. 00:09:02.710 --> 00:09:15.340 OK so for now let's stick to arrays and let's do that for all the the arrays 00:09:15.370 --> 00:09:21.660 . 00:09:28.390 --> 00:09:31.390 OK so you're getting only CJC varies. 00:09:31.490 --> 00:09:38.530 So if we look at the first results is corresponds to the first record on our table or the first row 00:09:38.680 --> 00:09:40.140 of our result sets. 00:09:40.270 --> 00:09:41.470 Can we can check that. 00:09:41.900 --> 00:09:43.100 We've got Peter. 00:09:43.960 --> 00:09:44.990 OK. 00:09:45.130 --> 00:09:54.490 And the second one also Peter Thaila and then the third one you call Mark Zuckerberg and so on. 00:09:54.500 --> 00:09:58.760 So once we arrived to call number six. 00:09:58.850 --> 00:10:00.700 So we don't see anything on our screen. 00:10:00.930 --> 00:10:01.690 Okay. 00:10:01.780 --> 00:10:06.870 And that's because this value here is no. 00:10:07.150 --> 00:10:17.090 And we can check that by guessing the type of these of these value are these variable or whatever the 00:10:17.090 --> 00:10:23.870 function is returning here by saying get type Okay. 00:10:24.200 --> 00:10:27.100 Are going to place us in the arguments. 00:10:27.110 --> 00:10:40.580 Okay so now if we save and refresh we can see that the loss was because of the function is choli fetch 00:10:40.640 --> 00:10:42.270 array is not. 00:10:42.530 --> 00:10:44.140 Okay. 00:10:44.240 --> 00:10:44.850 All right. 00:10:44.990 --> 00:10:54.740 So we're going to use this to cycle through our results sets. 00:10:55.000 --> 00:10:55.980 Okay. 00:10:56.020 --> 00:11:03.600 And preens all the rows okay inside our result sets. 00:11:03.620 --> 00:11:14.110 So rather than them printing this many times okay we're going to use a while loop and the condition 00:11:14.140 --> 00:11:18.340 of our while loop is going to be the outputs of this function. 00:11:18.760 --> 00:11:19.740 Okay. 00:11:19.780 --> 00:11:21.780 So they just copy this for now. 00:11:23.680 --> 00:11:32.970 And it just delete all this and then we're going to have a one loop. 00:11:33.130 --> 00:11:36.490 So the condition is going to be this. 00:11:36.700 --> 00:11:37.420 Okay. 00:11:37.540 --> 00:11:40.090 Well the because of our feature a function