The free MySQL Database is very often used with PHP.
--------------------------------------------------------------------------------
[color=FA0309]Connecting to a MySQL Database[/color]
Before you can access and work with data in a database, you must create a connection to the database. In PHP, this is done with the mysql_connect() function. [color=FA0309]Syntax[/color]
Note: There are more available parameters, but the ones listed above are the most important. [color=FA0309]Example[/color]
In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:
The connection will be closed as soon as the script ends. To close the connection before, use the mysql_close() function.
Before you can access and work with data in a database, you must create a connection to the database. In PHP, this is done with the mysql_connect() function. [color=FA0309]Syntax[/color]
mysql_connect(servername,username,password);
Note: There are more available parameters, but the ones listed above are the most important. [color=FA0309]Example[/color]
In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
--------------------------------------------------------------------------------
[color=FA0309]Closing a Connection[/color]$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
The connection will be closed as soon as the script ends. To close the connection before, use the mysql_close() function.
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>