denise28

做不成学术帝,就一辈子做吃货~
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Perl Connect to MySQL

Posted on 2012-02-03 15:49  denise28  阅读(248)  评论(0编辑  收藏  举报
  • Things we need to do before we continue our work:

  Make sure you install the right DBI.

  Perl DBI for Mysql : http://www.cs.wcupa.edu/docs/mysqlEZinfo/perl_dbi.html

      Install Perl DBI : http://cpansearch.perl.org/src/RUDY/DBD-mysql-2.9008/INSTALL.html#windows

Without DBI, you may encounter eorrors like: "Can't locate mysql.pm in @INC....."

  • Connect to Mysql and return value of data.

 

#!/usr/bin/perl

use mysql;

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

# MYSQL CONFIG VARIABLES
$host = "localhost";
$database = "test";
$tablename = "test_set";
$user = "";
$pw = "";

# PERL MYSQL CONNECT()
$connect = Mysql->connect($host, $database);

# SELECT DB
$connect->selectdb($database);

# DEFINE A MySQL QUERY
$myquery = "SELECT * FROM $tablename";

# EXECUTE THE QUERY
$execute = $connect->query($myquery);

$rownumber = $execute->numrows();
$fieldnumber = $execute->numfields();

# PRINT THE RESULTS
print "row:".$rownumber."\n";
print "col:".$fieldnumber."\n";