---
title: "Connecting PHP script with MySQL"  
description: "Before you can access data in a database, you must create a connection to the database. In PHP, this is done with the mysql_connect() function.  Synta"  
author: "Anonymous User"  
published: 2011-09-19  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/734/connecting-php-script-with-mysql  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# Connecting PHP script with MySQL

Before you can [access data](https://www.mindstick.com/forum/159853/how-to-access-data-using-entity-framework-in-asp-dot-net-mvc) in a database, you must create a [connection](https://yourviews.mindstick.com/view/83497/a-40-year-old-man-has-been-arrested-in-connection-with-the-abe-shooting) to the database. In PHP, this is done with the mysql_connect() function.\

##### Syntax:

## mysql_connect($server, $username, $password, $new_link, $client_flags);

Where the [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp), $server specifies the [server name](https://answers.mindstick.com/qa/110129/how-to-change-discord-server-name), $username specifies the username, $password specifies the password, $new_link is used If a second call is made to mysql_connect with the same [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments), no new link will be established, but instead, the link identifier of the already opened link will be returned(it is [optional parameter](https://www.mindstick.com/interview/1310/how-do-i-simulate-optional-parameters-to-com-calls)) and $client_flags is optional parameter which specifies a combination of the following [constants](https://www.mindstick.com/interview/22939/what-are-constants-in-objective-c): 128 (enable LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL etc.

Let’s have an example, how to create connection with [MySQL database](https://www.mindstick.com/forum/390/how-to-create-mysql-database-backup-file-using-php) server in PHP.

##### Example: Creating connection with MySQL database

```
<?php    //  creatingconnection with mysql database server      $con = mysql_connect("localhost", "root", "root");    // check connection is establish or not    if(!$con)    {        die ("Connectionnot established!  Please try again");    }    else    {        echo 'Connection successfully created.';    }?>
```

Now debug the above PHP script, output will be.

![Connecting PHP script with MySQL](https://www.mindstick.com/mindstickarticle/1ac687d9-f163-4632-8a07-c703b42d00ca/images/087d2273-d2b6-4b6e-bf2c-95eddf813f66.png)

##### Example: Closing a connection

If you do not close connection then connection will be close automatically after the executing script. But if you want to close a connection before finishing the script then write the following code:

##### Syntax:

mysql_close($conVariable);

```
<?php    //  creatingconnection with mysql database server      $con = mysql_connect("localhost", "root", "root");    // check connection is establish or not    if(!$con)    {        die ("Connectionnot established!  Please try again");    }    // write code      // Closing the connection    mysql_close($con);    echo 'Connection closed successfully';?>
```

##### Output:

![Connecting PHP script with MySQL](https://www.mindstick.com/mindstickarticle/1ac687d9-f163-4632-8a07-c703b42d00ca/images/e3f8bb4b-e9e5-456e-933b-e17df63b061f.png)

So this is the simple way through which we can create connection and close connection with MySQL database server.

\

---

Original Source: https://www.mindstick.com/articles/734/connecting-php-script-with-mysql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
