PHP MySQLConnect

Posted by Monir Hossain On 3:53:00 pm | No comments

PHP Connect to the MySQL Server


Use the PHP mysqli_connect() function to open a new connection to the MySQL server.

Open a Connection to the MySQL Server

Before we can access data in a database, we must open a connection to the MySQL server.
In PHP, this is done with the mysqli_connect() function.

Syntax

mysqli_connect(host,username,password,dbname);

ParameterDescription
hostOptional. Either a host name or an IP address
usernameOptional. The MySQL user name
passwordOptional. The password to log in with
dbnameOptional. The default database to be used when performing queries
Note: There are more available parameters, but the ones listed above are the most important.
In the following example we store the connection in a variable ($con) for later use in the script:
<?php
// Create connection
$con=mysqli_connect("example.com","peter","abc123","my_db");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>


Close a Connection

The connection will be closed automatically when the script ends. To close the connection before, use the mysqli_close() function:
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_close($con);
?>


0 comments:

Post a Comment

Blogroll

E COMMERCE

BANGLA PAPER

ENGLISH PAPER

Blogger news

About

E COMMERCE

BANGLA PAPER

ENGLISH PAPER