How to use Mysqli_connect API in PHP?

MySQLi is the improved version of MySQL database management system, it was introduced in MySQL version 4.3.0 and since than it’s been used everywhere. In the upcoming PHP version 5.6, MySQL_connect will no longer usable. Therefore, every PHP web developer should use the NEW MySQLi API while connecting to a database. Unlike the old MySQL_Connect, the new improved version has support for OOP (Object Oriented Programming) which is the future of web development. PHP as a server side scripting language has become more important after the OOP concept. So, in this tutorial we’ll learn how can we use MySQLi_connect in PHP very easily. \/\/

Before going to read further, I’ll recommend you to watch these free PHP courses: 

mysqli-and-php

How to establish a connection using MySQLi_connect?

Before the MySQLi, we used to establish a connection to the database in PHP using following syntax:

<?php mysql_connect(“server_name”,”user_name”,”password”);

?>

And for database selection we used to do this:

<?php

mysql_select_db(“database_name”);

?>

So it would become like this in two lines:

<?php

mysql_connect(“server_name”,”user_name”,”password”);

mysql_select_db(“db_name”);

?>

Now using the new syntax we can select all parameters at once like this:

<?php

mysqli_connect(“server_name”,”user_name”,”password”,”db_name”);

?>

This has saved an extra line of code for PHP developers. However the above example is just a normal example of a new connection, the appropriate connection looks something like this:

<?php
$con = mysqli_connect(“server_name”,”user_name”,”password”,”db_name”);

if (mysqli_connect_errno())
{
echo “connection was not established ” . mysqli_connect_error();
}
?>

In the above example, we’ve included the MySQLi_errno() function which actually checks whether the connection is successfully established or not, if the connection is not established then it will generate an error with our text and will display that in the browser, so the developer can know the issue. And we used $con which is a local user defined variable, which is very necessary in MySQLi because we need to add two parameters while executing a MySQL Query. Therefore, we must have the connection saved in a local variable, so we can give it as a reference to the query command while fetching data from the database.

How to use all MySQLi actions together?

Now we’ll use all the MySQLi functions at once, because we always have to use different MySQL actions in order to fetch or insert information/data into the database. Let’s have a look at this full example, I assume we have a table of users inside the database:

<?php
$con = mysqli_connect(“server_name”,”user_name”,”password”,”db_name”);if (mysqli_connect_errno())
{
echo “connection was not established ” . mysqli_connect_error();
}$query = “select * from users”;$run_query = mysqli_query($con,$query);

$row_query=mysqli_fetch_array($run_query) or $row_query=mysqli_fetch_assoc($run_query);

mysqli_close($con);

?>

The above is an example of working in a real environment, However, if you observe the mysqli_query function which has two parameters, that’s mean you must add the connection variable ($con) as a reference to the database and the $query to the query itself, so after that it will work fine. And in last we’ve closed the connection using the new MySQLi_close command.

How to use the MySQLi_connect in a Function?

If you are trying to use MySQLi with the same syntax as above inside a function in PHP then it will not work, it will generate an error, because inside the function there is a local scope for variables, so you can not use variables outside the function. And therefore, it’s very necessary to understand using mysqli_connect inside a function, the very simplest thing to do is; just include the db.php file into the function page and then simply use global command to make the variable usable in local scope inside a function, here is the simple syntax:

<?php

include(“db.php”);

function myfun(){

global $con;

$query = “select * from users”;

$run_query = mysqli_query($con,$query);

$row_query=mysqli_fetch_array($run_query) or $row_query=mysqli_fetch_assoc($run_query);

mysqli_close($con);

}

?>

so the above is the simplest syntax for using MySQLi_connect inside a function, the db.php is a separate file in which the database connection is saved, if you want to use the database connection on the same page then you can do something like this:

<?php
$con = mysqli_connect(“server_name”,”user_name”,”password”,”db_name”);if (mysqli_connect_errno())
{
echo “connection was not established ” . mysqli_connect_error();
}function myfun(){global $con;

$query = “select * from users”;

$run_query = mysqli_query($con,$query);

$row_query=mysqli_fetch_array($run_query) or $row_query=mysqli_fetch_assoc($run_query);

mysqli_close($con);

}

?>

The above is an example code if you want to use the connection on the same page. I hope, You’ve understood about using MySQli_connect in PHP, if you have any question then please use our comment form to ask. However, I didn’t mention anything about PDO (PHP DATA OBJECT) method in this tutorial, because I’ll later explain that in a different tutorial. PDO is also a part of MySQLi and directly related to Object Oriented Programming in PHP. Stay tuned.

About Abdul Wali

Abdul Wali is a professional blogger, marketer and web developer working online for last 5 years, he's been deeply involved in PHP, Web Development, SEO and computer video tutorials in different languages i.e English, Urdu and Pashto. You can catch him on and add him on Facebook.

Check Also

web hosting

The Best Web Hosting Providers (Tutorial)

Web hosting is one of the important aspects for establishing a business or website online. If ...

  • Wali Bhai,
    I created registration in php with database now I want to send the registration form to someone with database so, I exported DB in sql form phpmyadmin, now how do I import and run in another pc (localhost)?

    • Hi,
      Just import it using the localhost phpmyadmin on another PC, and it should work perfectly.

      • Thanks! It worked.
        Could you please how to show user email and brower in mail
        For Example if any user submits my contact us that I received in email and i also want to know the IP and brower of that User!

  • Naresh15

    what blog for language learning …

    very helpful .
    thank you bro…

    • You welcome brother, thanks for visiting!