There are various ways to make a MySQL Connection using PHP and each of them is 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:-

<?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 -:

<?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-:

<?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 -:

<?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.

 
Was this answer helpful? 0 Users Found This Useful (0 Votes)