PHP MySQL tutorial - Connecting to MySQL
In this PHP MySQL tutorial I will show you how to use basic database manipulation features in PHP.
|Name:||PHP MySQL tutorial|
Bookmark PHP MySQL tutorial
Step 2 - Connecting to MySQL
PHP MySQL tutorial
To use any database in PHP - and in other programming languages as well - you first need to connect to the database server. Connecting to a MySQL database in PHP requires some information. You need to get the following data to establish the connection:
- database username
- database name
Connecting to a database first you need to connect to the MySQL database server and in the next step you need to select the required database. Of course if the connection to the server failed then it makes no sense to continue the script so we can exit. Let's see how it looks like in PHP:
As you can see the mysql_connect() function establish the connection to the MySQL server and as next step we can select the database with mysql_select_db() function. If you execute the script the output should be the following:
Code:Connection to the server was successful! Database was selected!
Closing the connection
Before we go on I want to mention the opposite site as well. I mean closing a database connection. From PHP 4 you don't really need to close a database connection opened the way above. This is done automatically by the PHP garbage collector. However it makes no problem if you add the closing statement to your code. You can close a MySQL database connection calling the function mysql_close(). This function closes the actual database connection.
Previous Step of PHP MySQL tutorialNext Step of PHP MySQL tutorial
Tags: php mysql tutorial, php mysql, php database, php, mysql, tutorial
|PHP MySQL tutorial - Table of contents|
|Step 1 - PHP MySQL introduction|
|Step 2 - Connecting to MySQL|
|Step 3 - Retrieving data from the database|
|Step 4 - Insert, update, delete records|
|Step 5 - PHP MySQL database example|