Using MySQL Database in Your Application

To connect to MySQL database, you need to specify the following in your application.

  • Hostname: localhost or 127.0.0.1. Or if it is possible, use UNIX socket /var/lib/mysql/mysql.sock

  • Database name: specify the name of database you want to connect to. For example: u777_database.

  • Username: the name of database. In other words, this is the same as your database name above.

  • Password: the password of this database where can be set inside SiteManager.

The following examples illustrates how to use MySQL database inside your application, assuming the database to be used is u777_database with password 'PASSWORD'.

Tip

You can also see examples inside SiteManager. Go to Database Server and then click examples for the database in question.

Using MySQL Database With PHP

<?php
// Code taken from PHP manual

$username = "u777_database";
$password = "PASSWORD";
$hostname = "localhost";

// Connecting, selecting database
$link = mysql_connect($hostname, $username, $password);
mysql_select_db("my_database");

// Performing SQL query
$result = mysql_query($sql);

// Closing connection
mysql_close($link);
?>

Using MySQL Database With Perl

# code taken from Perl DBI documentation

use DBI;

# connecting to database
my $username = 'u777_database';
my $password = 'PASSWORD';
my $dbh = DBI->connect("DBI:mysql:$username",
    "$username", "$password");

# executing a single query without result
my $rv = $dbh->do($sql);

# retrieving result from a query
my $arrayref = $dbh->selectall_arrayref($sql);

# disconnect from database
my $rc = $dbh->disconnect();

Using MySQL Database With Python

import MySQLdb

# connecting to database
username = "u777_database"
password = "PASSWORD"
hostname = "localhost"
conn = MySQLdb.Connect(host=hostname,
    user=username, passwd=password, db=username)

# create result/cursor object
cursor = conn.cursor()

# perform a query
cursor.execute(sql)

# get result set
result = cursor.fetchall()

# close the connection
conn.close()

Using MySQL Database With ASP

connect_string = "Driver={Mysql}; " & _
   "Server=localhost; Database=u777_database; " & _
   "UID=u777_database; PWD=PASSWORD"

' opening connection to database
set dbConn = server.createObject("ADODB.connection")
dbConn.open connect_string

' perform a query
Set recordset = dbConn.Execute(SQL)

' closing connection
dbConn.Close

Copyright © 2003 indoglobal.com

. .