Solved

Microsoft Access Database and PERL...Using the select tag?

Posted on 2002-05-14
10
1,047 Views
Last Modified: 2013-12-25
I'm new to DBs and really, really suck at PERL. I need to have my database online. It has three columns, but only two will be displayed at this time. My question is, how do I get the two columns from Access to the web page? I tried looking for an answer, but all I found was:
SELECT *
FROM TABLE;

I know * is the column and TABLE is the table, but where do I go from there?
0
Comment
Question by:sjaguar13
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 10

Expert Comment

by:rj2
Comment Utility
To select only two fields you can use

SELECT Field1, Field2 FROM TABLE;
0
 

Author Comment

by:sjaguar13
Comment Utility
So I have a cgi script like this:

#!C:\perl
SELECT Field1, Field2 FROM TABLE;
Print "Field1 = $Field1";
Print "Field2 = $Field2";
0
 
LVL 10

Accepted Solution

by:
rj2 earned 200 total points
Comment Utility
Below is sample CGI script that demonstrates getting data from access. To make it work you must first create a odbc data source named test. The .mdb file must be saved below
your wwwroot. You must also install the module DBI::ODBC (if not already installed).

You might also want to check out MySQL (http://www.mysql.org) instead of Access.
MySQL is free and is a much better database than Access.


#!/usr/bin/perl

use DBI;
use CGI ':standard';

print header,start_html('Database test');
# Connect To Database
$db = DBI->connect( "dbi:ODBC:test", "", "",{RaiseError => 1, PrintError => 1, AutoCommit => 1} )
or die "Unable to connect: " . $DBI::errstr . "\n";

# Execute a Query
# * executing a query is done in two steps. First,
# * the query is setup using the "prepare" method.
# * this requires the use of the variable used to
# * initiate the connection. Second, the "execute"
# * method is called, as shown below.
$query = $db->prepare("SELECT field2,field3 FROM test");
$query->execute;

# How many rows in result?
# * the "rows" method using the variable name the
# * query was executed under returns the number
# * of rows in the result.
$numrows = $query->rows;

# Display Results
# * the fetchrow_array method executed on the
# * query returns the first row as an array.
# * subsequent calls return the other rows in
# * sequence. It returns zero when all rows have
# * been retrieved.
print "<TABLE border=1><TR><TD>Field2</TD><TD>Field3</TD></TR>\n";
while (@array = $query->fetchrow_array) {
($field2, $field3) = @array;
print "<TR><TD>$field2</TD><TD>$field3</TD>\n";
}
print "</TABLE>";
print end_html;

# Cleaning Up
# * with the DBI module, it is a good idea to clean up by
# * explicitly ending all queries with the "finish" method,
# * and all connections with the "disconnect" method.
$query->finish;
$db->disconnect;

exit(0);
0
 
LVL 12

Expert Comment

by:lexxwern
Comment Utility
first to start using databases with perl you need to install the DBI module.

DBI stands for Database Interface.

first strat your Perl Package Manager, you should get a prompt like this

ppm>

type the following command

ppm> install DBI

this should install the perl DBI module

now you can start using the DBI module in your scripts. and before you start you must master the sql, it is pretty easy.

and then this link will be of help,
http://www.mysql.com/doc/P/e/Perl_DBI_Class.html
0
 

Author Comment

by:sjaguar13
Comment Utility
Ok, I can't get this to work. What is PPM? Because I don't think I have that. I use Apache with Windows 98...how do I install the DBI thing?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Expert Comment

by:lexxwern
Comment Utility
1. look for a file named "ppm.bat" in your perl/bin directory.
2. run that file.
3. ppm>install DBI.

Thats All
0
 
LVL 10

Expert Comment

by:rj2
Comment Utility
sjaguar13,
You have installed ActiveState Perl, right?
If not, download it from URL below and install it.
http://www.activestate.com/Products/Download/Get.plex?id=ActivePerl

After you have installed it, start up a DOS prompt, and type commands as shown below (as lexxwern suggested).
PPM is a tool that comes with ActiveState Perl you can use to install Perl modules. Press enter after each command
(after installing ActiveState Perl you should have path to ppm.bat).

ppm
install DBI
install DBD::ODBC
exit

Then enter control panel, doubleclick on ODBC, click on "System DSN" property sheet, then click button "Add".
Select "Microsoft Access Driver (*.mdb), and click "Finish".
Type "test" as data source name, click button "Select" and select the Access .mdb file you want to use. The .mdb file should be located below the root of the web server.

If you still have problems we need to know exactly what kind of problems you're having in order to help you. Include information about what have you done so far, and what kind of error message you get (if any).
0
 
LVL 15

Expert Comment

by:samri
Comment Utility
sometime ppm might not work (well at least on my machine), try ppm3 instead.
0
 

Expert Comment

by:tonygreer
Comment Utility
If you are using a windows machine why not use IIS and ASP... Much simpler

Code:

<html>
<head>
      <title>Access Database</title>
</head>

<body>

<table border="1">
<%
'Create objects to connect to database
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRs = Server.CreateObject("ADODB.RecordSet")

'Connect to database and execute query
objConn.Open "ODBC Name Here"
objRs.Open "SELECT FieldName1, FieldName2 FROM TableName", objConn

'Loop through records
Do Until objRs.EOF
      Response.Write "<tr>"
      Response.Write "<td>"& objRs("FieldName1") &"</td>"
      Response.Write "<td>"& objRs("FieldName2") &"</td>"
      Response.Write "</tr>"
Loop

'Close connection to database
objRs.Close
objConn.Close

'Kill of objects
Set objRs = Nothing
Set objConn = Nothing
%>
<table>

</body>
</html>


Simple... Create a text file, put that code in it and then access it through its URL.
0
 

Expert Comment

by:tonygreer
Comment Utility
Sorry last comment should have been:

"Simple... Create a text file, put that code in it, save it as whatever.asp and then access it through its URL.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now