In this lecture, we will be connecting to the MySQL service with PHP. To do so, we will be using
mysqli_connect() function of PHP.
So let's start.
In order to connect to MySQL with PHP we need to change the authentication method of MySQL service to change
it, connect your MySQL service
and by using following command.
Changed authentication method.
Now we are using my mysql native password method.
And this "developer" string is my password of my user, so you can use anything you want.
For the next step, we will be changing ownership of the root directory of Apache service.
To do so, use following command
We are changing ownership according to our current user, my current user is developer so use your own user
name here and here and the root directory of Apache is /var/www/html/
And now we can start coding.
Open your Atom Editor
And.
Open the root directory of the Apache.
Inside the Atom Editor
And we are ready.
Create a new file.
I will call it conn.php
And inside the conn.php file
Connect to MySQL service using mysqli_connect() function.
Let's create a variable, I will call it $mysql and we will be using
mysqli_connect() function it takes four arguments. First argument to be the hostname is for the localhost
Second argument will be the user name of the MySQL service.
It will be "root" and the third one will be the password of MySQL service.
In my case, its developer and the fourth one is database name.
We will be connecting to the control panel database.
And in case an error occurs, we will be using mysqli_connect_errno() function. If any error has occurred,
we will return an error message.
And append the real error message next to it.
And exit the program.
Let's see.
Let's test Allcott.
Open up your browser and go to localhost, it would be your own host and slash conn.php
As you can see, no error message has been shown, so we have successfully connected to MySQL service
with PHP.
That's it for this lecture.
See you in the next one.