Use the PHP mysqli_connect() function to open a new connection to the MySQL server.
Before we can access data in a database, we must open a connection to the MySQL server.
In PHP, this is done with the mysqli_connect() function.
| Parameter | Description |
|---|---|
| host | Optional. Either a host name or an IP address |
| username | Optional. The MySQL user name |
| password | Optional. The password to log in with |
| dbname | Optional. The default database to be used when performing queries |
Note: There are more available parameters, but the ones listed above are the most important.
In the following example we store the connection in a variable ($con) for later use in the script:
The connection will be closed automatically when the script ends. To close the connection before, use the mysqli_close() function:
Your message has been sent to W3Schools.