PHP - SPLessons

PHP Connect to MySQL

Home > Lesson > Chapter 31
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Connect to MySQL

PHP Connect to MySQL

shape Description

PHP Connect to MySQL will gives the description about how to connect PHP to Database. In order to connect any database with PHP, a particular connection method is required. PHP has two popularized databases. The common features between the above two connection methods are:


shape Description

Here MySQLi database connection procedure is used after PHP5 versions in place of MySQL. In MySQLi database connections there are two procedures:

Create database using phpMyadmin

shape Description

After completion of XAMPP download and installation, creation of database and tables in it are done using phpMyadmin.

Steps for creating User Account, Database and Table

Step-1 : Navigate to http://localhost/phpmyadmin in the browser or Windows user can directly go to XAMPP control Panel and click on MySQL -> Admin which takes to phpmyadmin homepage which looks like below. Step-2 : Then go to User Accounts -> Add account -> Enter username, password, hostname -> Click in check box Check All -> Go as shown in below image. Step-3 : Then create a new Database by clicking New -> enter Database Name -> Select Collation -> Create like shown below. Step-4 : Then start creating a table in the database by giving the table name Authors -> No.of columns:4 -> Go Step-5 : The final structure of the table looks like below with the various values in the table.

Connecting to Database

shape Examples

MySQLi (object-oriented) // Before going to do this, create a database and get HostName from phpmyadmin panel. [php] <!DOCTYPE html> <html> <body> <?php $dbPassword = "PHPFundamentals"; $dbUserName = "PHPFundamentals"; $dbServer = "localhost"; $dbName = "PHPFundamentals"; $connection = new mysqli($dbServer, $dbUserName, $dbPassword, $dbName); print_r($connection); if($connection->connect_errno) { echo "Database Connection Failed: ".$connection->connect_errno); } ?> </body> </html> [/php] MySQLi Procedural [php] <!DOCTYPE html> <html> <body> <?php $dbPassword = "PHPFundamentals"; $dbUserName = "PHPFundamentals"; $dbServer = "localhost"; $dbName = "PHPFundamentals"; $connection = mysqli_connect($dbServer, $dbUserName, $dbPassword, $dbName); print_r($connection); if($connection) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?> </body> </html> [/php]

Closing the Connection

shape Description

The connection to the database closes automatically when the script ends.In order to close the connection before itself, the following statements can be used: MySQLi (object-oriented) [php]$connection->close();[/php] MySQLi Procedural [php]mysqli_close($connection);[/php]


shape Key Points

PHP Connect to MySQL draws out the following main points:
  • PHP has two popularized databases : PDO and MySQLi
  • MySQLi has the ability to be procedural instead of Object Oriented.
  • Create Database using PHPMyAdmin.