I have made a MySQL library for PERL, using the DBI interface, but I'm
having problems catching errors!
If there is a SQL-error the browser-window just turnes white and thats not
that informative!
As you can see, I've made a function that receives a $sql with the
SQL-Statement in and a reference to @data.
My function then exec the SQL and puts the returned data in the array @data.
This works just fine, but how (and where) should I
insert code for catching a SQL error and maybe print it to the screen or
even better mail the error to me???
Best regards
Thomas Haggren
Denmark
Here is my code:
$sql = qq�
SELECT data
FROM table
where someting = 1�;
&sql_select_cols( $sql, \@data);
#
# sql_select_cols
#
sub sql_select_cols
{
# Define vars
my($dbh, $sth, $numberOfRows, $i, @row);
# Get params from function call
my($sql, @params) = @_;
# Use DBI - Database independent interface for Perl
use DBI;
# Make connection to database
if(!$dbh)
{
$dbh = DBI->connect($DSN, $USER, $PASSWORD, { RaiseError => 1,
PrintError => 1,
AutoCommit => 1} );
}
# Clear arrays that ref is pointing at
for($i=0; $i < scalar(@params); $i++) { undef(@{$params[$i]}); }
# Prepare and execute sql
$sth = $dbh->prepare("$sql");
$sth->execute();
# Clear number of rows
$numberOfRows = 0;
# Loop results and return data through params
while (@row = $sth->fetchrow_array)
{
# Assemble all rows in one var separated by ;;
my($tabelfields) = join(";;",@row);
# Loop all params parses
for($i=0; $i < scalar(@params); $i++)
{
# Split $tabelfields at ;; and store data in the right param
my(@temp) = split(/;;/, $tabelfields);
$params[$i]->[$numberOfRows] = $temp[$i];
}
# Increase number of rows
$numberOfRows++;
}
# If you wish to know the number of exec sql-statements on a page, you
can alert $sql_select_cols_counter
if(!$sql_select_cols_counter) { $sql_select_cols_counter = 1; }
else { $sql_select_cols_counter++; }
# Disconnect from database
$dbh->disconnect;
# Return number of rows
return $numberOfRows;
}