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 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 connection error

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','[email protected]')");

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


    mysqli_close($conn);
?>
mysqli query output

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','[email protected]')");

    $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 array function 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','[email protected]')");

    $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

mysqli fetch assoc function 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','[email protected]')");

    $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

mysqli num rows function output

Conclusion

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

Enjoy Coding 🙂

One thought on “MySQLi Functions Introduction in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
MySQLi Functions Introduction in PHP
How to Create a Table in PHPMyAdmin

Here we discuss some basic steps on how to use phpMyAdmin. Before we started, let's get some basic information about...

Close