LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Perl Mysql Database connection (https://www.linuxquestions.org/questions/linux-newbie-8/perl-mysql-database-connection-737365/)

big 07-02-2009 11:36 PM

Perl Mysql Database connection
 
How do i connect mysql database using perl. Will like to see sample codes so i can quickly try it out. Thanks

vinaytp 07-03-2009 12:41 AM

Quote:

Originally Posted by big (Post 3595099)
How do i connect mysql database using perl. Will like to see sample codes so i can quickly try it out. Thanks

use DBI;
$database='nameofdatabase';
$host='localhost';
$port='3306';
$user='root';
$password="";
$dsn = "DBI:mysql:database=$database;host=$host;port=$port" or die "Connection Error: $DBI::errstr\n";
$dbh = DBI->connect($dsn, $user, $password);
$sql = "show tables";
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";

hope this helps........


All times are GMT -5. The time now is 08:35 AM.