Connect PHP to MySQL Database
Opening a connection to MySQL database from PHP is easy. Just use the mysql_connect() function like this
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'petstore';
mysql_select_db($dbname);
?>
$dbhost is the name of MySQL server. When your webserver is on the same machine with the MySQL server you can use localhost or 127.0.0.1 as the value of $dbhost. The $dbuser and $dbpass are valid MySQL user name and password.
Don't forget to select a database using mysql_select_db() after connecting to mysql. If no database selected your query to select or update a table will not work.
| Sometimes a web host will require you to specify the MySQL server name and port number. For example if the MySQL server name is db.php-mysql-tutorial.com and the port number is 3306 (the default port number for MySQL) then you you can modify the above code to :
$dbhost = 'db.php-mysql-tutorial.com:3306'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); It's a common practice to place the routine of opening a database connection in a separate file. Then everytime you want to open a connection just include the file. Usually the host, user, password and database name are also separated in a configuration file. An example of config.php that stores the connection configuration and opendb.php that opens the connection are : // This is an example of config.php
// This is an example opendb.php So now you can open a connection to mysql like this :
// ... do something like insert or select, etc |
Closing the Connection
The connection opened in a script will be closed as soon as the execution of the script ends. But it's better if you close it explicitly by calling mysql_close() function. You could also put this function call in a file named closedb.php.
mysql_close($conn);
?>
Now that you have put the database configuration, opening and closing routines in separate files your PHP script that uses mysql would look something like this :
// ... do something like insert or select, etc
include 'closedb.php';
?>
Creating Connections to Oracle Databases
' Visual Basic
Public Sub ConnectToOracle()
Dim conn As New OracleClient.OracleConnection()
' TODO: Modify the connection string and include any
' additional required properties for your database.
conn.ConnectionString = & _
"Data Source=;Integrated Security=yes"
Try
conn.Open()
' Insert code to process data.
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
Finally
conn.Close()
End Try
End Sub
// C#
public void ConnectToOracle()
{
OracleClient.OracleConnection conn =
new OracleClient.OracleConnection ();
// TODO: Modify the connection string and include any
// additional required properties for your database.
conn.ConnectionString = "Data Source=" +
";Integrated Security=yes";
try
{
conn.Open();
// Insert code to process data.
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
finally
{
conn.Close();
}
}
Creating Connections to ODBC Data Sources
' Visual Basic
Public Sub ConnectToOdbc()
Dim conn As New System.Data.Odbc.OdbcConnection
' TODO: Modify the connection string and include any
' additional required properties for your database.
conn.ConnectionString = & _
"FIL=MS Access;DSN=valid data source name"
Try
conn.Open()
' Insert code to process data.
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
Finally
conn.Close()
End Try
End Sub
// C#
public void ConnectToData()
{
System.Data.Odbc.OdbcConnection conn =
new System.Data.Odbc.OdbcConnection ();
// TODO: Modify the connection string and include any
// additional required properties for your database.
conn.ConnectionString = "FIL=MS Access;DSN=valid data source name";
try
{
conn.Open();
// Process data here.
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
finally
{
conn.Close();
}
}
Creating Connections to Access Databases
' Visual Basic
Public Sub ConnectToAccess()
Dim conn As New System.Data.OleDb.OleDbConnection()
' TODO: Modify the connection string and include any
' additional required properties for your database.
conn.ConnectionString = & _
"Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & _
"C:\Documents and Settings\username\My Documents\dbFile.mdb"
Try
conn.Open()
' Insert code to process data.
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
Finally
conn.Close()
End Try
End Sub
// C#
public void ConnectToAccess()
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for your database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= C:\Documents and Settings\username\" +
@"My Documents\AccessFile.mdb";
try
{
conn.Open();
// Insert code to process data.
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
finally
{
conn.Close();
}
}
Creating Connections to SQL Server
' Visual Basic
Public Sub ConnectToSql()
Dim conn As New SqlClient.SqlConnection
' TODO: Modify the connection string and include any
' additional required properties for your database.
conn.ConnectionString = & _
"integrated security=SSPI;data source=SQL Server Name;" & _
"persist security info=False;initial catalog=northwind"
Try
conn.Open()
' Insert code to process data.
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
Finally
conn.Close()
End Try
End Sub
// C#
public void ConnectToSql ()
{
System.Data.SqlClient.SqlConnection conn =
new System.Data.SqlClient.SqlConnection ();
// TODO: Modify the connection string and include any
// additional required properties for your database.
conn.ConnectionString =
"integrated security=SSPI;data source=SQL Server Name;" +
"persist security info=False;initial catalog=northwind";
try
{
conn.Open();
// Insert code to process data.
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
finally
{
conn.Close();
}
}