in reply to Using mysql with perl

Hi Kiat,

Here is a short bit of code to try and see if everythings working as it should, you'll have to change to your needs...

#!/usr/bin/perl -w use strict; use DBI; my $dbtype = "mysql"; my $dbname = "mydb"; my $dbuser = "username"; my $dbpass = "userpassword"; my $dbh = DBI->connect("DBI:${dbtype}:${dbname}", "$dbuser", "$dbpass" +) || die $DBI::errstr; my $sql = "SELECT foo, bar FROM mytable"; my $sth = $dbh->prepare($sql); $sth->execute() || die $DBI::errstr; while (my ($foo, $bar) = $sth->fetchrow()) { print "Column foo contains: $foo\n"; print "Column bar contains: $bar\n"; } $sth->finish(); $dbh->disconnect() || warn $DBI::errstr;

Hopefully that will get you started.
barrd

Replies are listed 'Best First'.
Re: Re: Using mysql with perl
by kiat (Vicar) on Jun 07, 2003 at 12:32 UTC
    Thanks, barrd! I saved your code as sql_test.cgi and ran it off the browser but I don't see any output. What could be wrong? Do I've to activate mysql or something?
      No problem, first off see rnahi's excellent reply/update above about setting up a username/password. And if you used your browser to activate the script then a blank page is what you would expect as the script is designed to be run from the command line... sorry, I should have pointed that out. It was for test purposes only to ensure that you could get "something" out of the MySQL DB.

      barrd