iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

July 3, 2008
Poll: Which Web editor do you use?
About
 
July 3, 2008
Book Review: Head First JavaScript
WebReference.com
 
July 3, 2008
10 Things You Can Do With a Wiki
About
 
July 2, 2008
Web Host Reviews - 10 New Reviews
About
 
July 2, 2008
14 Reasons You Should Join a Social Network
About
 
July 1, 2008
Mark Boulton's Freelance Design Secrets
SitePoint
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /CGI and Perl

Perl And MySQL; Using DBI; Connections 

  Views:    11642
  Votes:    2
by Gyan Kapur 11/27/03 Rating: 

Synopsis:

Perl abstracts databases using DBI, its database independent interface. Writing code using DBI is simple, for a programmer who has used Perl before, but for novices it can be a scary experience. This tutorial series aims at making the transition from flatfiles, or any other type of database to DBI a bit simpler. This article will focus on how to connect to a MySQL database, what it means, and what one can do from there.
Pages: firstback1 3 forwardlast
The Article

Opening The Connection [Page 2]

Well, we'll begin with the basic connect statement:

$dbh = DBI->connect($dsn, $user, $password,
                     { RaiseError => 1, AutoCommit => 0 });

This line establishes a database connection ($dbh, which stands for database handle), from which we'll able to query the database in the future.  Each parameter that the connect method of the DBI module (which is actually what OO programmers call a constructor) takes is important.  The connect method takes a data source ($dsn), MySQL username ($user), MySQL password ($password), and a hash reference with additional parameters, such as whether to throw errors when the driver has errors, or whether to automatically commit data for transactional databases.

The Data Source

   $dsn      = 'dbi:mysql:dbname=NameOfDatabase';

$dsn must contain 'dbi:driver_name:', from there the input is database specific. With MySQL 'dbname=NameOfDatabase' should be all that's necessary.  At other times, you may need to specify the host you wish to connect to, if you're not trying to connect locally.  In MySQL, as well as most databases, you'll have to specify which database you're connecting to in $dsn, because MySQL access is specified on a per database level, and sometimes even on a per table level (in GRANT tables.)  Assuming you do have access, you still need a username and password to connect.

Usernames And Passwords

  $user     = 'mysqlusername';
  $password = 'mysqlpassword';

Although MySQL generally attributes access on a per database level, users are created for all of MySQL.  Therefore, you are entering a MySQL username and password.  MySQL then proceeds to connect you to the specific datasource you requested, and grant you the access privileges your username and password afford you.

Final Hashref

Assuming you'll be able to connect to the database, you can then modify some attributes in this final hashref.  According to Tim Bunce, the hashref can be used  'alter the default settings of PrintError, RaiseError, AutoCommit, and other attributes,' even your mysql username and password (taking precedence over the previously specified ones.)

Pages: firstback1 3 forwardlast

Similar/related articles:


 
  Sponsors