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 phpMyAdmin quick tutorial.

NOTE: MySQLi functions are only useful to connect with MySQL databases.

mysqli_connect()

This function opens a new connection to 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 Functions

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 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 Functions

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 Functions

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 Functions

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 Functions

One thought on “MySQLi Functions Introduction in PHP

Leave a Reply

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