PHP

MySQLi Functions Introduction in PHP

MySQLi functions allow you to access the MySQL database server. Here we take a quick look at some basic functions of them. If you are not familiar to use phpMyAdmin please refer to the phpMyAdmin quick tutorial.

MySQLi Functions in PHP

There are lots of MySQLi functions in PHP. MySQLi functions are useful to connect with MySQL databases.

mysqli_connect()

This function opens a new connection to the MySQL server.

Syntax

mysqli_connect(host,username,password,DBname);

Example

<?php
    $conn = mysqli_connect("localhost","user_name","your_password","db1");
    //check connection
    if(!$conn)
    {
        die("connection error...".mysqli_connect_error());
    }else{
        echo "hi";
    }
?>

Output

mysqli_connect_error()

This function returns the error description from the last connection error.

Syntax

mysqli_connect_error();

Example

<?php
    $conn = mysqli_connect("localhost","g_k","your_password","db1");
    //check connection
    if(!$conn)
    {
        die("connection error...".mysqli_connect_error());
    }else{
        echo "hi";
    }
?>

Output

mysqli_query()

This function performs a query against the database.

Syntax

mysqli_query(connection, query);

Example

<?php
    $conn = mysqli_connect("localhost","user_name","your_password","db1");
    //check connection
    if(!$conn)
    {
        die("connection error...".mysqli_connect_error());
    }else{
        echo "hi";
    }
    
    //performing queries
    mysqli_query($conn,"INSERT INTO t1 (FirstName,LastName,Email) 
    VALUES ('Alex','Standal','elexs@gmail.com')");

    $result = mysqli_query($conn,"SELECT * FROM t1");
    


    mysqli_close($conn);
?>

mysqli_fetch_array()

This function fetches a result row as an associative array, a numeric array, or both.

Syntax

mysqli_fetch_array(result,resulttype);

Example

<?php
    $conn = mysqli_connect("localhost","user_name","your_password","db1");
    //check connection
    if(!$conn)
    {
        die("connection error...".mysqli_connect_error());
    }else{
        echo "hi...";
    }
    
    //performing queries
    mysqli_query($conn,"INSERT INTO t1 (FirstName,LastName,Email) 
    VALUES ('Alex','Standal','alexs@gmail.com')");

    $result = mysqli_query($conn,"SELECT * FROM t1");
    
    $sql="SELECT LastName FROM t1 ORDER BY LastName";
    $result=mysqli_query($conn,$sql);

    //Numeric array
    $row=mysqli_fetch_array($result);
    echo $row[0]." ".$row[1];

    // Free result set
    mysqli_free_result($result);
    
    mysqli_close($conn);
?>

Output

mysqli_fetch_assoc()

This function fetches a result row as an associative array.

Syntax

mysqli_fetch_assoc(result);

Example

<?php
    $conn = mysqli_connect("localhost","user_name","your_password","db1");
    //check connection
    if(!$conn)
    {
        die("connection error...".mysqli_connect_error());
    }else{
        echo "hi...";
    }
    
    //performing queries
    mysqli_query($conn,"INSERT INTO t1 (FirstName,LastName,Email) 
    VALUES ('Alex','Standal','alexs@gmail.com')");

    $result = mysqli_query($conn,"SELECT * FROM t1");
    
    $sql="SELECT LastName FROM t1 ORDER BY LastName";
    $result=mysqli_query($conn,$sql);

    //Associative array
    $row=mysqli_fetch_assoc($result);
    echo $row["LastName"];

    // Free result set
    mysqli_free_result($result);
    
    mysqli_close($conn);
?>

Output

Must Read: Mysqli_fetch_assoc vs Mysqli_fetch_array [With Example]

mysqli_num_rows()

This function returns the number of rows in a result set

Syntax

mysqli_num_rows(result);

Example

<?php
    $conn = mysqli_connect("localhost","user_name","your_password","db1");
    //check connection
    if(!$conn)
    {
        die("connection error...".mysqli_connect_error());
    }else{
        echo "hi...";
    }
    
    //performing queries
    mysqli_query($conn,"INSERT INTO t1 (FirstName,LastName,Email) 
    VALUES ('Alex','Standal','alexs@gmail.com')");

    $result = mysqli_query($conn,"SELECT * FROM t1");
    
    $sql="SELECT LastName FROM t1 ORDER BY LastName";
    $result=mysqli_query($conn,$sql);

   if ($result=mysqli_query($conn,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);
  echo "Result set has ". $rowcount ." rows";
  // Free result set
  mysqli_free_result($result);
  }

    mysqli_close($conn);
?>

Output

Read Also: Prepared Statement Using MySQLi

Conclusion

I hope now you have a great understanding of MySQLi functions in PHP. You can explore some more MySQLi functions.

Enjoy Coding 🙂

Nachiket Panchal

Founder & Administrator of `errorsea` Having interest in Programming & Technology.

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

2 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

3 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

3 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

3 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

3 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

3 years ago