Adscend

Click Here



Click Here



Click Here

Tuesday 3 September 2013

Create a Connection to a MySQL Database

1.The mysql_connect()function is used to do the connection to MySQL database program.

2.The mysql_connect()function returns a resource which is a pointer to the database connection.


Syntax
mysql_connect(servername/hostname,username,password);

Here, servername/hostname: 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 who owns the server process.

Password:Optional. Specifies the password to log in with. Default is "".


Create a file named “connect.php” within /xampp/htdocs.
Point your browser to http://localhost/connect.php. May you see like this:
Example: 1
connect.php
<?php
$con = mysql_connect("localhost","","");
if ($con){
echo "Connection is succeed";
}
else
{
die("Could not connect: " . mysql_error());
}
?>
Output
Connection is succeed

Example: 2
connect.php
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$con = mysql_connect("$hostname", "$username", "$password");
if ($con){
echo "Connection is succeed";
}
else
{
die("Could not connect: " . mysql_error());
}
?>
Output
Connection is succeed

No comments:

Post a Comment