• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1085
  • Last Modified:

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

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
sjaguar13
Asked:
sjaguar13
  • 3
  • 2
  • 2
  • +2
1 Solution
 
rj2Commented:
To select only two fields you can use

SELECT Field1, Field2 FROM TABLE;
0
 
sjaguar13Author Commented:
So I have a cgi script like this:

#!C:\perl
SELECT Field1, Field2 FROM TABLE;
Print "Field1 = $Field1";
Print "Field2 = $Field2";
0
 
rj2Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
lexxwernCommented:
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
 
sjaguar13Author Commented:
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
 
lexxwernCommented:
1. look for a file named "ppm.bat" in your perl/bin directory.
2. run that file.
3. ppm>install DBI.

Thats All
0
 
rj2Commented:
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
 
samriCommented:
sometime ppm might not work (well at least on my machine), try ppm3 instead.
0
 
tonygreerCommented:
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
 
tonygreerCommented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now