Introduction to MySQL

PHP and MySQL : Home
Introduction to MySQL
mysqli class
Creating Connection
Performing Query
Handling Results

MySQL is an open source DBMS(Database Management System) used by the most small scale users to some of the biggest enterprises.
Database is a collection of structured data (data in tables). MySQL is a relational database i.e. data is stored in many tables instead of one table and these tables may have relations existing in between.

SQL(Structured Query Language) is the most common database querying langugae and for this tutorial you should have basic knowledge about SQL.
MySQL provides API for most of the programming language and PHP and MySQL is the most commonly used database - programming language combination.

Procedural and Object Oriented Interface
PHP has both procedural and object oriented methods for connecting to MySQL. We will recommend to use object oriented methods.
The difference in these two method is that in Object oriented method we create and object first and then call functions and perform tasks. mysqli class of PHP provides the interface to connect and query in MySQL. Here is a program to demostrate the difference in Procedural and Object Oriented methods :

// Lets create tables using both methods

// Object Oriented
// Creating object
$mysqli_object = new mysqli("localhost","username","password","database");
$sql1 = "CREATE TABLE data1(column1 INT, column2 INT) ";

// Procedural Method
$connect = mysqli_connect("localhost","username","password","database");
$sql2 = "CREATE TABLE data2(column1 INT, column2 INT) ";

Here we created two tables data1 using Object oriented method and data2 using Procedural method. You may not able to visualize the benifits of using object oriented method here but on large programs you will see it's benifits.

In Object oriented Method we

  1. Created a object of mysqli class and passing agruments to constructor.
  2. Calling query function which is a member function of class mysqli and passing query as argument

Just understand the difference between these two methods we will explain all other things in later tutorials.

Login to Track Your Progress