Connect PHP to MySqli Using OOP Approach


In this article we will see how to connect PHP to MySQLi using OOP approach. Now in this article, you will get understand how to connect your my SQL database with PHP using object-oriented programming. The object-oriented programming adjusts the structure of class and object. using this approach we will connect the PHP to the MySQLi server. In the subsequent articles, we will show you how to insert update delete and search data from the database.

In this article, we are using PHP 7.2 version. There is a different way to connect to your PHP program to MySQL server but this is one of the most amazing ways to connect your PHP to MySQL. The reason behind this is you can update all the properties of the class and change the database and also you can change the table.  

We are not getting deep into the class which helps us to update the whole properties but we are just showing you the basic concept of the class where you can connect PHP to MySQL.

So let’s get started.

 

Create PHP Class

Now to connect PHP to MySQLi database using oop approach You need to create PHP class file. The expansion of this file is ( .php).  after creating this file you need to insert keyword class and specify the class name.

Now just look at the example.

No just wanted to explain the above code.

  • we first create a class using class keyword and specify the class name.
  • Then in the curly bracket will specify the properties of the class.  so we create some properties like a $server, $user, $Password, and database name. 
  • we specify the default value to the class properties. So I just specify localhost value to the server property, root value to the user property. If you have a different username then you can change the root value to your name.  and if you using the domain then just change the localhost with your domain name. Then I specify the password blank. I don’t set any password to my server so I just specify blank string here. then select the database using $db and specify your database name.
  • After that, we create a constructor. But first I just wanted to explain what is the constructor if you already know what is the constructor then just skip this few rows.  so the constructor is the function that executes when we create the instance of the class. we have an article of object-oriented programming so just read this article for more detail.  so when you create an instance of the class this function automatically execute and initialize your connection property.
  • In this constructor, we created if and else statement. We first initialize the connection property. So to initialize your connection property you need to specify the parameter to the MySqli function. The first parameter is going to be the server name, then the second is the username. Third in the password, and forth in the database. We just specify the important parameter to the connection property there is parameter more than this.
  • Then we check if the connection is the valid connection then we print connection successful message on the screen.

 

Create Instance of Object

Now I just wanted to show you how you can access this method and how you can implement the class. To access all the methods of the class you first need to create the instance of the class. we call this instance as an object. Using this object you can access all the properties and methods of the class.

So this is this simple Syntax of creating an instance of the class. If you specify the parameter to the constructor you need to specify that parameter when you creating the instance. we will just talk about this in the future tutorials.

Now how to access the functions in the class. Now we have one more article that explains to you how to access the functions how to create properties and how to access the properties using the instance of the class. You must read that article.

now when you run this file you will see connection successful message if your connection is the valid connection. Otherwise, the application will send the error message.

So that is all for this article. If you find anything useful this article then must share this article and bookmark this website.

Leave a Reply

Your email address will not be published. Required fields are marked *