PHP MYSQLi Object Oriented Tutorial for Beginners

If you are following my blog you notice that in all my posts I use procedural mysqli instead of using PDO or mysqli object oriented class. I know working with OOP is quite difficult for beginners but if you try to understand and learn basics of object oriented programming I am sure you will be start using php mysqli class extension.

In this tutorial I am going to show you how to connect, select, insert, update and delete records using built-in php mysqli class with basic examples.

I will not cover prepared statement in this post. If you have basic understanding of MYSQLi and want to learn prepared statement then read my post “PHP MYSQLi Object Oriented Prepared Statements for beginners”

PHP MYSQLi Object Oriented

Create Database Connection:

If database is not connected then I will Show error number and error message and stop script execution

Get Current Database Charset:

Check Mysqli Client Version:

Select Statement:

Get Num Rows:

Get Fields Count:

Get Affected Rows:

Note: Num rows and fields count values will get from result object and Affected Rows value will get from connection object.

Fetch Single Row:

fetch_row() get result as an enumerated array


Fetch Associative Array:

Fetch Object:

Fetch All Records:

fetch_all method return all records found in query. It takes 1 argument with 3 different types. MYSQLI_ASSOC return associative array, MYSQLI_NUM returns numeric array and MYSQLI_BOTH returns both associative and numeric array.

Free Results:

Free() method frees the memory associated with a result.

Insert record in Mysql Table:

Get Inserted Id:

Insert record using real escape string:

Update record in Table:

Delete record in Table:

Close Mysqli Database Connection:

Also read:


Posted in PHP

Author: Ahsan Zameer

I am a web developer and love to search new stuff on web. Trying different approaches and ideas to make web developing more interesting and enjoyable.