PDA

View Full Version : newbie trying to learn question


CarlosSanchez
12-16-2002, 10:27 PM
I'm trying to do this problem: Create a perl program that reads in information from the "cartable" table of the Access database file. We will use the SQL query string "SELECT * FROM cartable".
Assign that string to a variable. Create a connection to the database "cars.mdb" using the ODBC DBD and the "cars" DSN.

Print out a header saying that this is the Automobile database, and putting headers on the columns (ID, Year, Make, Model, Color, Price).

Prepare the SQL statement based on our earlier string. Execute the query.

Create a while {} loop to display the result set.

Here's what I have so far:

$db = DBI -> connect("dbi:ODBC:cars") || die "Couldn't open DB";
$st = $db -> prepare ("SELECT * FROM cartable");
while (@row = $st -> fetchrow_array)
{
print "$row[2]\n";
}
$db -> disconnect;


My question in, How would I print the title and header? Any help is appreciated. Thanks!

Grizz2
12-17-2002, 01:28 AM
#First you need this line in your script
print"Content-type:text/html\n\n";

# then put your html between the "anyword tags" with no space between the
# first "anyword tag" and your html,,
#and your last html and last "anyword tag".
# you can use normal html between these words
# and also use your perl variables.

print<<"PrintTag";
<html>
<head></head>
<body>
<h1>Put whatever you want in the html</h1>
<table>
<tr>
<th>ID</th><th>Year</th><th>Make</th>
<th>Model</th><th>Color</th><th>Price</th>
</tr>
PrintTag
# you can break anywhere and start doing perl stuff again

$db = DBI -> connect("dbi:ODBC:cars") || die "Couldn't open DB";
$st = $db -> prepare ("SELECT * FROM cartable");
while ($row = $st -> fetchrow_array)
{
# probably need to split this row into seperate variables
# notice I changed @row to $row above
# I used a pipe here, you probably would do this different in Access but just get
# your fields seperated so you can have more flexibility using them


($id,$year,$make,$model,$color,$price) = split(/\|,$row);
# next statement should be on one line.
print "<tr><td>$id</td><td>$year</td><td>$make</td><td>$model</td><td>$color</td><td>$price</td></tr>";

}
$db -> disconnect;
# then go back and finish up your html

print<<"PrintTag";
</table>
</body>
</html>
PrintTag
exit;

Thats the general idea. You can play around and work out the details.

Grizz

Hawk989s
01-03-2003, 07:40 AM
It is not necesary to split the array into separate scalars. You just have to remember that the first number in a perl array is [0], so the first column in $array[0]