I was wondering if anyone could help me solve this problem. The script appears not to be connecting to the database. This script prints "Here we go" but never prints "Database connected". I used the format that the hosting company supplied. "DBI:mysql:$database:localhost","$username","$password"" Could it be that I use underscores in my database and user names?
#!/usr/bin/perl -w
use strict; use DBI; use CGI;
my ($dbh, $sth); my $database = ''; my $username = ''; my $password = '';
print "Content-type: text/html \n\n"; print "<html><head><title>Database Output</title></head>"; print "<body><p>"; print "Here we go<br>";
$dbh = DBI -> connect("DBI:mysql:$database:localhost","$username","$password") or die $DBI::errstr;
print "Database connected<br>";
$sth = $dbh->prepare ("CREATE TABLE test (first_field CHAR(10), second_field INT)");
$sth = execute();
print "Table created <br>";
$sth = $dbh->prepare ("INSERT INTO test VALUES ('1new line',1), INSERT INTO test VALUES ('2NEW LINE',2), INSERT INTO test VALUES ('3NEW LINE',3)");
$sth = execute();
print "Rows inserted<br>";
$dbh = shift; my $count;
$sth = $dbh->prepare ("SELECT * from test"); $sth = execute();
$count = 0;
while (my $row = $sth->fetchrow_hashref()) { print "$row = {first_field} $row->{second_field}<br>"; ++$count; }
print "</p>"; $sth = finish();
print "<p>No items are found.</p>" if $count == 0; print "</body></html>";
exit(0);