There are various ways to make a MySQL Connection using PHP and each of them are described in the following text.

To connect MySQL Using MySQL Improved (mysqli) Extension :

To connect MySQL use the below PHP code and to Select a Database:-

Code:
<?php
$mysqli = new mysqli("localhost", "username", "password", "dbname");
?>
Here replace username, password and dbname with your username, password and database name.

To connect MySQL use the below PHP code and to Select a Database

MySQL Improved Extension can be used only with MySQL databases while PDO abstracts database access and allows the user to create code which can handle various types of databases.

Use the below PHP Code to Connect to MySQL and to Select a Database -:

Code:
<?php
$myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');
?>
Here replace username, password and dbname with your username, password and database name.

To connect MySQL using Legacy MySQL (mysql_) Functions :

You can connect MySQL by using below PHP code and to Select a Database-:

Code:
<?php
mysql_connect('localhost','username','password');
mysql_select_db("dbname");
?>
Here replace username, password and dbname with your username, password and database name.

To connect remote MySQL Database using PHP :

You can connect MySQL by using below PHP code and to Select a Database -:

Code:
<?php
$mysqli = new mysqli("servername", "username", "password", "dbname");
?>
Here replace username, password and dbname with your username, password and database name. Replace the servername with the Hosting Server Name.
Author
bhawanisingh
Views
2,071
First release
Last update
Rating
0.00 star(s) 0 ratings
Top