PHP form tutorial Phase14

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:
• hostname
• database username
• password
• 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:
Code:
1.
8.
PHP F1

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!
PHP F1
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.



Explore more on PHP form creation and handling :


0 comments:

Post a Comment