The free MySQL database is very often used with PHP.
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.
| Parameter | Description |
|---|---|
| servername | Optional. Specifies the server to connect to. Default value is "localhost:3306" |
| username | Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process |
| password | Optional. Specifies the password to log in with. Default is "" |
Note: There are more available parameters, but the ones listed above are the most important. Visit our full PHP MySQL Reference for more details.
In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:
The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function:
| WEB HOSTING |
|---|
| Best Web Hosting |
| PHP MySQL Hosting |
| Best Hosting Coupons |
| UK Reseller Hosting |
| Cloud Hosting |
| Top Web Hosting |
| $7.95/mo SEO Hosting |
| Premium Website Design |
| WEB BUILDING |
|---|
| XML Editor - Free Trial! |
| FREE Website BUILDER |
| Free Website Templates Free CSS Templates |
| Make Your Own Website |
| W3SCHOOLS EXAMS |
|---|
|
Get Certified in: HTML, CSS, JavaScript, XML, PHP, and ASP |
| W3SCHOOLS BOOKS |
|---|
|
New Books: HTML, CSS JavaScript, and Ajax |
| STATISTICS |
|---|
|
Browser Statistics Browser OS Browser Display |
| SHARE THIS PAGE |
|---|