Database is a collection of data that can be retrieved by the user through application program created for a specific problem. A database server is an application that allows creating and managing a database preferably for multiple users. Some database servers used with PHP are PostgresSQl, MySQL, SQL Server, Oracle. Database servers have one or more distinct APIs for allowing control over data through programs like creating, accessing, managing, searching and replicating. PHP connect to MySQL database requires the mysqli_connect function.

PHP-Connect to MySQL Database

Before inserting, updating or deleting the data in database tables a connection must be made with the database MySQL. In this regard the process is called to connect to the MySQL database server. PHP function mysqli_connect() is used which returns a database handler. Using the handler returned, any table stored in the database can be accessed or updated after selecting the required database created for the task.

Syntax of mysqli_connect()

mysqli_connect(servername, username, password, database)
• Server name is the name of the database server where the database is stored. Most commonly it is the localhost.
• Username and password- it is the name and password of an authorized user who is given right on a database. In case of MySQL localhost the default user is “root” with no password.
• Database is the name of database from where the user wishes to access the data.

Example

 
 
<?php
                $servername = "localhost";
                $database = "root";
                $username = "";
                $password = "myUserDB";
                // Create MySQL DB connection
                $conn = mysqli_connect($servername, $username, $password, $database);
                // Check the connection for success
                           if (!$conn)
                {
                   die("Connection failed: " . mysqli_connect_error()."-". mysqli_connect_error());
                }
                echo "Connected successfully";
                mysqli_close($conn);
?>

Other function related to PHP connect to MySQL

  • mysqli_connect_errno()-Returns the last error code number from the last call to mysqli_connect(). Zero means no error occurred.
  • mysqli_connect_error()-Returns connection error occurred while connecting with MySQL. It is a description in string for latest connection error. A string that describes the error. NULL is returned if no error occurred.
  • mysqli_change_user(string $user , string $password , string $database)- Changes the user of the specified database connection. Returns TRUE on success or FALSE on failure.
  • mysqli_select_db (string $dbname )- this function can be used in place of setting fourth parameter in mysqli_connect() so that any database can be connected with the connection when required if the data is scattered in more than one databases.
  • mysqli_close()-Closes a previously opened database connection. Returns TRUE on success or FALSE on failure.