Which of the following method can be used to close a mysql database using php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    MySQLi Procedural procedure:
    To close the connection in mysql database we use php function mysqli_close() which disconnect from database. It require a parameter which is a connection returned by the mysql_connect function.

    Syntax:

    mysqli_close(conn);

    If the parameter is not specified in mysqli_close() function, then the last opened database is closed. This function returns true if it closes the connection successfully otherwise it returns false.

    Below program illustrate the mysqli_close() function

    $servername = "localhost";

    $username = "username";

    $password = "password";

    $conn = mysqli_connect($servername, $username, $password);

    if (!$conn) {

        die("Connection failed: " . mysqli_connect_error());

    }

    $sql = "CREATE DATABASE newDB";

    if (mysqli_query($conn, $sql)) {

        echo "Database created successfully with the name newDB";

    } else {

        echo "Error creating database: " . mysqli_error($conn);

    }

    mysqli_close($conn);

    ?>

    MySQLi Object-oriented procedure::

    To close the connection in mysql database we use php function conn->close() which disconnect from database.

    Syntax:

    conn->close();

    Program: To illustrate the closing of connection in object-oriented procedure.

    $servername = "localhost";

    $username = "username";

    $password = "password";

    $dbname = "newDB";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {

        die("Connection failed: " . $conn->connect_error);

    }

    $conn->close();

    ?>

    Using PDO procedure:
    To close the connection in MySQL database in PDO procedure we set the connection name to null which disconnect from the database.

    Syntax:

    conn=null;

    Program: to illustrate the closing of connection in PDO procedure.

    $servername = "localhost";

    $username = "username";

    $password = "password";

    try {

        $conn = new PDO("mysql:host=$servername;dbname=newDB"

                         $username, $password);

        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "CREATE DATABASE newDB";

        $conn->exec($sql);

        echo "Database created successfully with the name newDB";

        }

    catch(PDOException $e)

        {

        echo $sql . "

    " . $e->getMessage();

        }

    $conn = null;

    ?>

    References: http://php.net/manual/en/mysqli.close.php


    Which of the following methods connect MySQL database using PHP Mcq?

    In PHP in order to access MySQL database you will use: mysqlconnect() function.

    Does PHP automatically close MySQL connection?

    Most CMSs close the MySQL connection at the end of the request, which is really meaningless, because PHP will do it anyway.

    Which of the following function is used to disconnect with MySQL database?

    bool mysql_close ( resource $link_identifier );

    How can we connect to a MySQL database from a PHP script?

    Create Database..
    Create a Folder in htdocs..
    Create Database Connection File In PHP..
    Create new php file to check your database connection..
    Run it..