MySQL Database Connectivity using PHP

MySQL Database Connectivity using PHP

Mysql is a database server, which is used to store data for long time and mysql in the form of phpMyAdmin come with a XAMP default setup where you can easily create a database and save data here and to show and manipulate that data connect that database with your web services and retrieve data to show it on the website.

What is MySQL? Why is MySql used?

  • It’s a database which is mostly used on the web.
  • This database runs on a server and saves data online.
  • Mysql is used in large and small school level projects
  • It is fast and reliable data source.
  • Standard structural query language is used in it.
  • Its and Open source Product.

As mysqli have two ways to program, which are object oriented approach and the other one is simple procedural programming. We demonstrate both of them in this post, how to connect to the database and then how to check the connection status while using PHP as a server side programming language.

This is consisting of three parts.

  • Create a Connection
  • Check Connection
  • Close Connection

Create a Connection (Object Oriented Mysqli):

Create a Connection (Procedural Mysqli):

Note: While using Wamp or Xamp the user name is root, otherwise it’s the one you select for it and the database variable contain the name of database and password contain null or “ “ if there is no password for the database set.

Check Connection (object oriented MySQLi):

Check Connection (procedural MySQLi):

Note: Remember one thing the connect_error in object oriented mysqli is an attribute of the mysqli class and the mysqli_connect_error() is function in procedural mysqli.

Close Connection (object oriented mysqli):

Close Connection (Procedural mysqli):