Connect to MySQL Server: mysql_connect()
Unlike other languages, PHP provides a simple function to connect to the mysql database. The function.
mysql_connect() is used to connect to a mysql database. This function returns the connection object.
Once a connection has been established the resulting connection object can be used to interact with the mysql server. The syntax of the function is explained below.
$conn = mysql_connect (“host”,”userid”, “password”);
The above PHP statements use the mysql_connect function to get the connection string. This function takes the following argument.
Hostname: – This is the address of the mysql server. If mysql server is installed on the local
machine (which is almost always true on Linux OS). This host is usually “localhost”.Userid: – This is the userid which should be used by the mysql_connect () function if password is needed to connect to the database. If mysql server is installed on your “localhost” then you can use the default user “root” as the userid. This user id has all the privileges on the mysql server.Password: – This is the password of the userid specified earlier. If you have not changed the password on your local machine then most probably this password is blank. You should use the “” to denote a blank password, instead of leaving this argument at all.
So, after you connect to the mysql server, it returns you a connection object. I prefer to call it connection string. So please do not be confused with this connection string if you see it again
Error Handling During MySQL Execution
Due to several possible reasons (invalid user id, password etc.) if some time the PHP script fails to
connect to the mysql server, it will throw an error. The error message can be retrieved by using the inbuilt function mysql_error (). This function returns the latest error. So most of the above connection statement is written like this
$conn = mysql_connect (“host”,” userid”, “password”)
The die function when called, stops the execution of the PHP code. This does not necessarily mean that no output is generated. Even if the PHP script dies, all the html code is sent to the browser. Only the PHP statements following the die function will not be executed.
Close MySQL Connectin: mysql_close()
To close any particular connection, we use the mysql_close() function. The syntax of this function is as below.
mysql_close() takes the connection object as argument. Though mysql server itself closes connection after sometime, It is a good practice to explicitly close any mysql connection using this function.
In actual scenario, one connects to the mysql database initially and close if only at last when all possible SQL statements have been executed. This ensures faster response time of the script as compared to connecting and disconnecting to mysql database for each sql statement.
*P5systems is one of the best Website Development Company provides you all afforadable end-to-end website design solutions
Sources: http://www.articlesbase.com/Tags: WEB