0 Replies - 1573 Views - Last Post: 12 October 2007 - 09:14 AM

#1 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6747
  • View blog
  • Posts: 31,227
  • Joined: 10-May 07

Connect to mysql database & retrieve data

Posted 12 October 2007 - 09:14 AM

Description: Required modules: DBI DBD::mysql Both of these are available from CPan.
# Use of MySQL
##############

use DBI;
#use DBD::mysql;

# Set the variables for the Database Connection
my $host = "dbtest";      # your database server, can be localhost
my $database = "Log";   # connecting database
my $table = "DownloadedFiles"  # table within the database
my $dbuser = "private";  # database login userid
my $dbpass = "private";  # database login password 

$dbh = DBI->connect("DBI:mysql:database=$database;host=$host",$dbuser,$dbpass) || die "nDB connection error!n";
$sth = $dbh->prepare("select * from $table where ID = '4455'");
$sth->execute();
while (@data = $sth->fetchrow_array()) {
	printf("ID: %sn",$data[0]);
	printf("Date: %sn",$data[1]);
	printf("Time: %sn",$data[2]);
	printf("Status: %sn",$data[3]);
	printf("Hash: %sn",$data[4]);
	printf("Company: %sn",$data[5]);
	printf("FileName: %sn",$data[6]);
	printf("FileSize: %sn",$data[7]);
	printf("Log Message: %sn",$data[8]);
}

# Required is DBI & DBD::mysql, both of these are available from CPan.


Is This A Good Question/Topic? 0
  • +

Page 1 of 1