-3

I am trying to connect to a MySQL database.

I found this script and I am trying to use it on my PC and web host, but it doesn't show any output.

Please have a look at this code. I am running perl at xampp.

#!C:\xampp\perl\bin\perl.exe

print "Content-type: text/html\n\n";

use DBI;
use strict;

my $driver   = "localhost"; 
my $database = "data";
my $dsn      = "DBI:$driver:database = $database";
my $userid   = "root";
my $password = "";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;

I am using the same database with PHP.

Borodin
  • 126,100
  • 9
  • 70
  • 144
NIrik Shan
  • 25
  • 1
  • 6

3 Answers3

3

but it doesn't show any output.

Judging by the CGI header you're displaying, I assume that you're running this as a CGI program. In which case, it's no surprised that you're not seeing any output - you're not sending any output.

If you ran it as a command line program (and it's often a good idea to get stuff working on the command line before leaping into CGI programming), then you would see the "Content-Type" header. Alternatively, you could add some output to your program and see if that appears in your browser. Something simple like:

print 'It works!';

I'd also like to add, that CGI looks rather outdated these days and there are far better (by which I mean easier and more powerful) ways to write web applications with Perl. You might like to read CGI::Alternatives to get an idea of what is available.

Update:

I've just seen this question asked on Facebook (please don't cross-post without telling people) and I've noticed that your $driver variable is wrong. If you're connecting to MySQL, then $driver should be "mysql" (so that DBI loads "DBD::mysql").

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
3

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

Borodin
  • 126,100
  • 9
  • 70
  • 144
-1

you could add:

if ($dbh) {
 print 'connect ok';
} else {
 print 'connect failed';
}