The Mason approach is based on MVC, therefore you need to define the database connection within your Base.mc as follows:
<%init>
my $conn = DATABASE_NAME::DBI->conn();
my $sconn = DATABASE_NAME::DBI->session_conn();
Afterwards e.g. in your index.mi you need to define another init section in which you can execute your actual query to gather informations from your Database:
<%init>
my $dbh = Ws21::DBI->dbh();
my $sth;
my @articles;
if (!defined $.search) {
$sth = $dbh->prepare("SELECT SOME QUERY FROM DATABASE_NAME");
$sth->execute();
}
Then you are able to call this information with perl from within your html code section like this:
% for my $article (@articles) {
...
% }
For this call it is very important that you start the perl section with % at the very beginning of the line. If it will be started with leading spaces or tabs, it will throw an exception.
To connect to a database from PERL you can use DBI, the Database independent interface. The following code snipped shows you how:
use DBI;
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "joe", "joe's password", {'RaiseError' => 1});
my $sth = $dbh->prepare("SELECT …. FROM articles WHERE article_id=?");
$sth->execute(1);
while(my @row = $sth->fetchrow_array()){
printf("%s\t%s\n%s\n",$row[0],$row[1],$row[4]);
}
$sth->finish();