Tag Archives: MySQLi Connection PHP

OOP MySQLi : Connect to your database

Why should you use MySQLI ?

Firstly, MySQLi stand for: MySQL Improved. Since PHP 5.5.0 old mysql extension is deprecated and will soon be removed.This is the first reason you should use mysqli over mysql.

Secondly and what I think it’s the most important and useful feature that mysqli give us is that you can use prepared statements for your queries.That is a much much safer way of querying your database and preventing sql injection.

Connecting to your database using MySQLi

Create a new file named something like connect.php, database.php or whatever your want.Now open it with your favourite text editor.

First thing we need to do is defining the variables which contain information of your database, of course you don’t need to define them but it’s a much cleaner and will may be helpful later in your projects.

Great, we are now ready to proceed to actually connecting to the database using the new mysql improved extension by adding this variable.

After this you are pretty much all set and you can test it on your localhost or webhost and if you get no errors and the page is blank then you connected successfully to your database.

I personally like to let it as it is without checking for errors because if there is any error php will automatically tell you that something went wrong.For example if you have a larger PHP project and you setup your database connection wrong, you can easily tell because of all the errors you will get all over the page if the database connection doesn’t work.

Thats it for today’s tutorial, I will probably do part 2 very soon for you guys.Thanks for reading !