A DSN for the MySQL driver looks like this
DBI:mysql:database=$database;host=$hostname;port=$port
where the host
and port
fields are optional. (The database
is also optional, but you don't want to leave that out.) There are several more esoteric options too, but they're irrelevant here
But you're supplying
DBI:localhost:database = data
Which doesn't even specify a MySQL connection, so I'm not surprised if it doesn't work! I don't know whether the spaces are legal, but I would leave them out to keep in line with the documentation.
You should change that statement to
my $dsn = "DBI:mysql:database=$database;host=$driver"
You may remove ;host=$driver
if you wish (why have you called the host name "driver"?) as localhost
is the default. A DSN that specifies just a database name and uses the default for all the other fields may be contracted to just
my $dsn = "DBI:mysql:$database"
It may be easier to just write print
statements at first to generate some output. You will want to print a MIME content type header of text/plain
instead of text/html
. Try print "$DBI::errstr\n"
for now instead of die
, as the latter writes to stderr which won't appear in your browser