Connecting to MySQL database
In order to access and manage records in a database, we need first to establish a connection to the database.
To connect to the MySQL database we are going to use the mysql_connect() function.
Here is the syntax:
mysql_connect (“$servername”,”$dbusername”,”$dbpassword”);
Parameters of mysql_connect() function:
servername:
The name of the computer or the ip address where the MySQL server resides, default value is localhost.
dbusername:
specifies the username to login or connect to MySQL, usually root.
dbpassword:
specifies the password for the user account in the server.
Here is an example script:
<?php $conn = mysql_connect("localhost","root","") or die("could not connect"); ?>
In the above example we have used the $conn variable to handle or store the connection. Our servername is localhost, dbpassword is root and leave it blank in the password field. The die clause is the error handler of our script, it will display “could not connect” if the connection fails.