#!/usr/bin/perl -w use strict; use DBI; # connect to the database and create a handle to the connection. my $dbh=DBI->connect('DBI:mysql:host=myhost.domain.tld;database=mydatabase', 'user','password') or die $DBI::errstr; #create a statement handle and prep our SQL my $sth=$dbh->prepare(qq( select user_id,username,password,given,surname from users_table order by user_id )) or die $dbh->errstr; $sth->execute(); while (my $row=$sth->fetchrow_arrayref){ # do something with it } | rest of code