---
title: "How to connect with database in PHP"  
description: "Before we can access the data in MYSQL database,we have to first connect the server.PHP provides mysqli_connect() function to connect with database. T"  
author: "Royce Roy"  
published: 2016-06-04  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11138/how-to-connect-with-database-in-php  
category: "php"  
tags: ["php", "mysql"]  
reading_time: 1 minute  

---

# How to connect with database in PHP

Before we can [access the data](https://answers.mindstick.com/qa/40056/what-is-the-weo-update-and-how-can-i-access-the-data) in [MYSQL database](https://www.mindstick.com/forum/392/how-to-restore-backup-file-in-mysql-database),we have to first connect the server.PHP provides **mysqli_connect()** [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [connect with database](https://www.mindstick.com/forum/34348/in-c-sharp-how-to-connect-with-database-and-looping-over-a-recordset). This function has five [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp):\

\

![How to connect with database in PHP](https://www.mindstick.com/blogs/8961561f-63a2-429e-b631-bcb115924559/images/69a9e9f7-ecce-4d31-b997-ee421a18f7db.png)\

##### Example: MYSQLi object-oriented

```
<?php // Create connection  $connect=new mysqli(“localhost”,”username”,”password”);  //check connection If($connect->connect_error){     die(“Connection failed :”  . $connect->connect_error);}else {     echo(“connection established”);}?>
```

**Note:** In above example $connect_error is used untill PHP 5.2.9 and 5.3.0. If you are using PHP version other than 5.2.9 and 5.3.0 use following code:

```
<?php // Create connection$connect = mysql_connect(“localhost””,”username”,”password”);  //Check connection If($connect->connect_error){     die(“Connection failed :”  . mysqli_connect_error());}echo(“Connection established”);?>
```

---

Original Source: https://www.mindstick.com/blog/11138/how-to-connect-with-database-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
