Connecting to your MySQL database with PHP, Perl, C#, VB.NET, or Classic ASP requires only a few lines of code once you have the correct host, database, username, and password. The samples below match the core connection patterns for each language or library exactly as provided in the original documentation.

These are kept minimal without built-in error or exception handling, as that must be added according to your application's specific logging and recovery needs. The examples assume your MySQL database and user have already been created in the hosting control panel.

#Prerequisites

  • MySQL database created with known hostname (often localhost), database name, username, and strong password
  • Appropriate language extensions or connectors enabled on the server (mysql, mysqli, PDO, Perl DBI/DBD::mysql, MySQL Connector/NET, and MyODBC are available)
  • Correct permissions granted to the MySQL user for the target database
  • Development environment or staging site ready for testing before production deployment

#PHP Connection Methods

PHP supports multiple MySQL interfaces. The traditional mysql extension, mysqli (both procedural and object-oriented), and PDO are shown. Use mysqli or PDO for new projects because the original mysql extension is deprecated.

#PHP - MySQL

php
<?php
$link = mysql_connect('myServerHost', 'myUser', 'myPassword');
mysql_select_db('myDatabase', $link);
// your queries here
mysql_close($link);
?>

#PHP - MySQLi (Procedural)

php
<?php
$conn = mysqli_connect('myServerHost', 'myUser', 'myPassword', 'myDatabase');
// your queries here
mysqli_close($conn);
?>

#PHP - MySQLi (OO)

php
<?php
$conn = new mysqli('myServerHost', 'myUser', 'myPassword', 'myDatabase');
// your queries here
$conn->close();
?>

#PHP - PDO

php
<?php
$conn = new PDO('mysql:host=myServerHost;dbname=myDatabase', 'myUser', 'myPassword');
// your queries here
$conn = null;
?>

#Perl Connection

Perl connects through the DBI module and DBD::mysql driver. The connect call accepts a DSN string containing the database and host, followed by username and password.

perl
use DBI;
use DBD::mysql;
$myConn = DBI->connect("dbi:mysql:myDatabase;myServerHost", "myUser", "myPassword");
# your queries here
$myConn->disconnect;

#.NET Connections

Both C# and VB.NET use the MySQL Connector/NET library. The connection string format is identical; only the language syntax differs. Always call Open before use and Close/Dispose afterward.

#C# - MySQL .Net Connector

csharp
using MySql.Data.MySqlClient;

MySqlConnection myConn = new MySqlConnection("Server=myServerHost;Database=myDatabase;Uid=myUsername;Pwd=myPassword");
myConn.Open();
// your commands here
myConn.Close();
myConn.Dispose();

#VB.Net - MySQL .Net Connector

vbnet
Imports MySql.Data.MySqlClient

dim myConn As MySqlConnection
myConn = new MySqlConnection("Server=myServerHost;Database=myDatabase;Uid=myUsername;Pwd=myPassword")
myConn.Open()
' your commands here
myConn.Close()
myConn.Dispose()

#Classic ASP Connection

Classic ASP uses ADODB with the MySQL ODBC 5.1 driver. The connection string must specify the exact driver name and parameters.

asp
<%
Dim sConnection, objConn, objRS
sConnection = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=myServerHost; DATABASE=myDatabase; UID=myUsername;PASSWORD=myPassword; OPTION=3"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open(sConnection)
' your queries here
objConn.Close
Set objConn = Nothing
%>

#Best Practices and Common Pitfalls

Hard-coded credentials in source files create security risks; move them to configuration files outside the web root. Connections must be closed explicitly to return resources to the server pool. Deprecated interfaces such as the original mysql extension should be avoided on long-lived applications.

  • Implement try/catch or On Error blocks around connection code
  • Use parameterized queries instead of string concatenation for SQL
  • Validate all inputs that reach the database layer
  • Check server error logs if a connection attempt fails

Test each snippet with a simple SELECT 1 query before adding business logic. Correct connection strings resolve the majority of initial problems.

Once connected successfully, you can proceed to run queries, process result sets, and implement the rest of your application logic.