1 00:00:00,480 --> 00:00:08,340 In this lecture, we will be connecting to the MySQL service with PHP. To do so, we will be using 2 00:00:08,670 --> 00:00:11,010 mysqli_connect() function of PHP. 3 00:00:12,270 --> 00:00:13,800 So let's start. 4 00:00:15,190 --> 00:00:24,460 In order to connect to MySQL with PHP we need to change the authentication method of MySQL service to change 5 00:00:24,460 --> 00:00:27,100 it, connect your MySQL service 6 00:00:33,910 --> 00:00:36,070 and by using following command. 7 00:00:37,270 --> 00:00:38,980 Changed authentication method. 8 00:00:41,250 --> 00:00:44,760 Now we are using my mysql native password method. 9 00:00:45,730 --> 00:00:51,900 And this "developer" string is my password of my user, so you can use anything you want. 10 00:00:53,930 --> 00:01:01,850 For the next step, we will be changing ownership of the root directory of Apache service. 11 00:01:03,280 --> 00:01:05,980 To do so, use following command 12 00:01:12,190 --> 00:01:21,100 We are changing ownership according to our current user, my current user is developer so use your own user 13 00:01:21,100 --> 00:01:30,190 name here and here and the root directory of Apache is /var/www/html/ 14 00:01:32,860 --> 00:01:34,930 And now we can start coding. 15 00:01:36,510 --> 00:01:37,950 Open your Atom Editor 16 00:01:44,050 --> 00:01:44,710 And. 17 00:01:45,700 --> 00:01:48,370 Open the root directory of the Apache. 18 00:01:49,820 --> 00:01:51,380 Inside the Atom Editor 19 00:01:57,140 --> 00:01:58,280 And we are ready. 20 00:01:59,480 --> 00:02:00,860 Create a new file. 21 00:02:02,630 --> 00:02:06,050 I will call it conn.php 22 00:02:07,330 --> 00:02:12,130 And inside the conn.php file 23 00:02:13,320 --> 00:02:18,810 Connect to MySQL service using mysqli_connect() function. 24 00:02:22,470 --> 00:02:28,620 Let's create a variable, I will call it $mysql and we will be using 25 00:02:29,780 --> 00:02:39,530 mysqli_connect() function it takes four arguments. First argument to be the hostname is for the localhost 26 00:02:40,700 --> 00:02:44,690 Second argument will be the user name of the MySQL service. 27 00:02:45,710 --> 00:02:53,270 It will be "root" and the third one will be the password of MySQL service. 28 00:02:54,180 --> 00:03:00,200 In my case, its developer and the fourth one is database name. 29 00:03:00,950 --> 00:03:03,860 We will be connecting to the control panel database. 30 00:03:07,560 --> 00:03:22,950 And in case an error occurs, we will be using mysqli_connect_errno() function. If any error has occurred, 31 00:03:24,960 --> 00:03:26,970 we will return an error message. 32 00:03:33,440 --> 00:03:37,480 And append the real error message next to it. 33 00:03:49,580 --> 00:03:51,080 And exit the program. 34 00:03:58,760 --> 00:03:59,510 Let's see. 35 00:04:01,300 --> 00:04:02,460 Let's test Allcott. 36 00:04:06,370 --> 00:04:15,400 Open up your browser and go to localhost, it would be your own host and slash conn.php 37 00:04:16,710 --> 00:04:23,880 As you can see, no error message has been shown, so we have successfully connected to MySQL service 38 00:04:23,880 --> 00:04:24,650 with PHP. 39 00:04:25,930 --> 00:04:27,190 That's it for this lecture. 40 00:04:27,400 --> 00:04:28,470 See you in the next one.