---
title: "Connection from PHP with MySQL"  
description: "Connection from PHP with MySQL"  
author: "Sandra Emily"  
published: 2023-07-27  
updated: 2023-07-28  
canonical: https://www.mindstick.com/forum/159320/connection-from-php-with-mysql  
category: "mysql"  
tags: ["php", "mysql", "database connection"]  
reading_time: 3 minutes  

---

# Connection from PHP with MySQL

[Connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) from [PHP](https://www.mindstick.com/articles/12149/php-constant-and-php-variables) with [MySQL](https://www.mindstick.com/articles/12156/what-is-mysql)

## Replies

### Reply by Aryan Kumar

Sure, you can connect from PHP with MySQL using the `mysqli` extension. The `mysqli` extension provides a set of functions that allow you to connect to a MySQL database and execute queries.

To connect from PHP with MySQL, you can follow these steps:

1. **Include the** `mysqli` **extension.** The `mysqli` extension is not enabled by default, so you will need to include it in your PHP code. You can do this by adding the following line to the top of your PHP file:

PHP

```plaintext
require_once 'mysqli.php';
```

1. **Create a new** `mysqli` **object.** The `mysqli` object represents a connection to a MySQL database. You can create a new `mysqli` object by calling the `mysqli_connect()` function. The `mysqli_connect()` function takes three arguments: the hostname of the MySQL server, the username, and the password. For example, the following code creates a new `mysqli` object and connects to a MySQL database on the local host with the username `root` and the password `password`:

PHP

```plaintext
$mysqli = new mysqli('localhost', 'root', 'password');
```

1. **Check the connection.** After you have created a new `mysqli` object, you should check the connection to make sure that it is successful. You can do this by calling the `mysqli_connect_errno()` function. The `mysqli_connect_errno()` function returns the error number if the connection failed, or 0 if the connection was successful. For example, the following code checks the connection to the MySQL database and displays an error message if the connection failed:

PHP

```plaintext
if ($mysqli->connect_errno) {
    echo 'Error connecting to MySQL: ' . $mysqli->connect_error;
    exit;
}
```

1. **Execute a query.** Once you have checked the connection, you can execute a query by calling the `mysqli_query()` function. The `mysqli_query()` function takes one argument: the SQL query. For example, the following code executes a query that selects all rows from the `products` table:

PHP

```plaintext
$result = $mysqli->query('SELECT * FROM products');
```

1. **Free the result.** After you have executed a query, you should free the result by calling the `mysqli_free_result()` function. The `mysqli_free_result()` function frees the memory that was used to store the results of the query. For example, the following code frees the result of the query that was executed in the previous step:

PHP

```plaintext
mysqli_free_result($result);
```

1. **Close the connection.** Once you have finished using the `mysqli` object, you should close the connection by calling the `mysqli_close()` function. The `mysqli_close()` function closes the connection to the MySQL database. For example, the following code closes the connection to the MySQL database:

PHP

```plaintext
mysqli_close($mysqli);
```


---

Original Source: https://www.mindstick.com/forum/159320/connection-from-php-with-mysql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
