MCQ question PHP

PHP connection with mysql database

PHP connection with MySQL database

PHP connection: –Hi Readers, Here  I am going to explain about how can we make a database connection with MYSQL in PHP Language.Users which are a web developer for them making a database connection is an important and interesting topic.MySql is an open source database management system to store and retrieve records.To do an operation on the database then, first of all, we need to make a connection with database successfully.Here I start a session on PHP connection with MySql database.

 

PHP provides mysqli_connect() function to open or make  a database connection. This function takes five parameters and returns a MySQLi link identifier on success or FALSE on failure.To connect to a database we need 3 parameters like the local path where the database is available and then username, password to make the connection.

MySQLi extension (the “i” stands for improved).

Server: – The host name or IP address running database server. If not specified, then the default value is localhost:3306.

Username: –  The username accessing the database. If not specified, then the default is the name of the user that owns the server process.default username of MySql in a local database is root.

Password: – Optional – The password of the user accessing the database. If not specified, then the default is an empty password on the local system.But if database is on remote server then it should  definitely  have password

 

EXAMPLE of PHP connection with MySql:-

<html>

<head>

<title>PHP connection with Mysql Database</title>

</head>

<body>

<?php

   $dbhost = 'localhost';

   $dbuser = 'root';

   $dbpass = '';

   $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

   if(! $conn )

   {

     die('Could not connect: ' . mysqli_error());

   }

   echo 'Connected successfully';

   mysqli_close($conn);

?>

</body>

</html>

 

 Mysqli_close() :- The mysqli_close() function closes a virtual path to connect the opened database connection establish.

Mysqli_error(): –   That function role is if the connection is not established between database and PHP.Then it shows the message of error.

 

 

OUTPUT:-

 PHP connection with mysql database

 

Here you see the example of establish successfully  PHP connection with database.

Here I end Session on PHP connection with the database.

Leave a Comment