MYSQL CONNECTION STRING

www.

How do I use a connection string to connect to a MySQL database?

Please note: This support article is a guide for our Linux users only.

The following article explains how to use a connection string to connect to a MySQL database. The syntax of the connection string will vary based on the programming language you are using. Below are sample connection strings for PHP, ColdFusion, Perl and JSP. The code snippets can be added directly to your code.

In the following examples, please substitute your information where the following data is referenced:

  • <server>: enter the MySQL server that you are assigned to, for example, mysql4.safesecureweb.com
  • <username>: enter the username provided for your database
  • <password>: enter the password provided for your database
  • <database>: enter the database name provided for your database
  • <DSN>: enter the DSN name (ColdFusion only)

PHP

<?php
$link = mysql_connect(‘<server>’, ‘<username>’, ‘<password>’);
if (!$link) {
  die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_select_db(<database>);
?>

ColdFusion

<CFQUERY Name="test" DATASOURCE="<DSN>" USERNAME="<username>"
PASSWORD="<password>">
</CFQUERY>

Perl

#!/usr/bin/perl

use DBI;

$db = DBI->connect("dbi:mysql:<database>","<username>","<password>")
or die("Couldn’t connect");

$db->disconnect;

JSP

<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>

<%!
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection conn;
conn = DriverManager.getConnection(
"jdbc:mysql://<server>/<database>?user=<username>&password=<password>");
%>

Comments are closed.