Reputation: 515
I am using Perl CGI to access a MySQL database in XAMPP with the following code:
#!/xampp/perl/bin/perl -w
use DBI;
print "Content-type:text/html\r\n\r\n";
$database = "mydb";
$host = "localhost";
$port = "3306";
$user = "root";
$pw = "";
$dsn = "dbi:mysql:$database:$host:$port";
print "Trying to connect <br />";
$connect = DBI->connect($dsn, $user, $pw, {RaiseError=>1});
if (defined $connect) {
print "Preparing query <br />";
}
$query = "SELECT * FROM reference WHERE ID = 1742031";
$query_handle = $connect->prepare($query);
$query_handle->execute();
$query_handle->bind_columns(undef, \$pmid, \$popu, \$loc);
while($query_handle->fetch()) {
print "$pmid, $popu, $loc <br />";
}
However I am unable to print out the result to my webpage. I think I am not being able to connect to the database because the "Preparing Query" line is not getting printed. But I can connect to the database and execute this query from the mysql command line. I have no idea what the problem could be.
EDIT: After installing the missing DBD:mysql module, I get a new error: "Can't connect to MySQL server on localhost (10061)". I tried following the suggestions here: MYSQL - Cant connect to MYSQL server on 'localhost' (10061) and everything seems to be fine. I have my mysqld.exe running and I am running mysql as a service. Also, I can connect to the database through the mysql command lineas well as phpMyAdmin, so there should be no privileges issue as I only have a root account.
Please advise. I will be grateful for any help.
Upvotes: 1
Views: 7596
Reputation: 173
I got the a similar issue. Actually, XAMPP is now using MariaDb instead of MySQL.
So you need to use:
'DBI:MariaDB:host=...'
in your connection string to make it work. Also check my answer from here: Why won't this Perl DBI/DBD MySQL (MariaDB) connect to locahost work?
Upvotes: 0
Reputation: 11546
How do I install this module?
Use "cpan" on the command line:
> cpan
cpan shell -- CPAN exploration and modules installation (v1.960001)
Enter 'h' for help.
cpan[1]> install DBD::mysql
If that isn't installed, you can download it:
https://metacpan.org/pod/DBD::mysql
Upvotes: 1
Reputation: 3219
To connect to a MySQL database with DBI, use
$dsn = "dbi:mysql:$database;host=$host;port=$port";
localhost and port 3306 are the default values, so you could leave those parts out.
Upvotes: 0