September 26, 2013 6:02 am

How to use MySQLi_connect in PHP

As you all knows that MySQLi is improved PHP extension of MySQL which is introduced in PHP 5. MySQL is deprecated in PHP 5.5 will be removed in future. We have millions of applications using PHP MySQL and needs to be updated on MySQLi or PDO (PHP Data Objects is also a new extension you can use to use in your applications). So today i am going to give you a very simple integration guide of MySQLi how to connect to a database, how to  run a query and insert records etc hope you love this article.

how-to-use-mysqli_connect-in-php

Example of mysqli connection with the object method:

Example of mysqli connection with the procedural method:

Connection error checking with object method:

Connection error checking with procedural method:

Escaping characters with mysqli real escape:

Simple procedure with complete code with procedural method:

Hope you like this tutorial love to reply on your comments.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • Husni’s Elemento

    PDO is secure and faster

    • huzoorbux

      We are moved to mysqli.

      • Husni’s Elemento

        can you give me a reason why you moved to mysqli..??
        I read some article about PDO and MySQLi. and they said that PDO more secure.

        • meryem

          and there ‘s problem with PDO when closing connection

  • Kieran

    I’d just like to point out the typo in mysqli_fecth_array, should be mysqli_fetch_array

    • huzoorbux

      Update it Thanks.

  • Thanx

  • Giang Nguyễn Ngọc Trường

    mysqli_fetch_array have other parameter

    $result=mysqli_fetch_array($querry, RESULT_TYPE);

    result we have look like:
    =======================
    id ======= name ==== class
    =======================
    1 ======= giang ==== 19
    ….

    if RESULT_TYPE
    +MYSQLI_NUM: access result array =>> $result[0]=1 , $result[2]=19

    +MYSQLI_ASSOC: access result array =>>> $result[‘id’]=1, $result[‘name’]=giang

    +MYSQLI_BOTH: will create a single array with 2 attribute you can use $result[0]=1 or $result[‘id’]=1

    Just a few notes for who read this 🙂

  • what does “or die” do during assignment and how come I can’t do “or thrown new Exception”?