Home / code / cgi

Database connection

If you want to connect to a database using ODBC, you can use the following code.

use Win32::ODBC;
if (!($db = new Win32::ODBC("DSN=databasename;UID=userid;PWD=password")))
{
   print "The connection has failed";
}
else
{
   $db->Sql("SELECT column1 FROM table");
	while ($db->FetchRow())
	{
	  ($name)=$db->Data("column1");
	  print "$name";
	}

   $db->Sql("SELECT column2 FROM table");
	while ($db->FetchRow())
	{
	  ($name)=$db->Data("column2");
	  print "$name";
	}
   $db->Close();
}

 

TOP

Latest script:

 

Books: