Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Perl/CGI Performance for a Shopping Cart

by Spenser (Friar)
on Aug 04, 2002 at 17:58 UTC ( [id://187514]=note: print w/replies, xml ) Need Help??


in reply to Perl/CGI Performance for a Shopping Cart

I used to use Perl with flat database files because I thought it was the simplest and best way to go.  Then I discovered mySQL--this reads like a laundry detergent commercial.  Anyway, mySQL has worked out much better for me.  It does take a bit of an effort to get started, but it's not too bad with the proper books and some patience.  As for its performance compared to other databases, it ranks as high as Oracle these days, according to e-week.

Regarding your concerns about exporting to delimited text files, that's a breeze with mySQL and Perl and the Perl DBI module.  As with everything else with Perl, there are many ways of doing it, but below is a simple overview of an export program.  This export program is assuming that you've already set up a database and table in mySQL, of course.

#!/usr/bin/perl -w use DBI; # Connect to the mySQL server and # get today's orders. my $dbh = DBI->connect("DBI:mysql:orders:localhost","user","passwd") || die "Could not connect:".DBI->errstr; my $sql_stmnt = "SELECT order_id, item_no, qty, price FROM orders WHERE order_date=CURRENT_DATE'"; my $sth = $dbh->prepare($sql_stmnt); $sth->execute(); while(@orders = $sth->fetchrow_array()) { $order_id = $orders[0]; $item_no = $orders[1]; $qty = $orders[2]; $price = $orders[3]; # Create record with bars as delimiters # and push record into array for exporting. $order = $order_id . "|" . $item_no . "|" . $qty . "|" . $price . " +\n"; push(@export, $order); } $sth->finish(); $dbh->disconnect(); # Now loop through array of orders # and save to text file. open(EXP, ">client-orders.txt") || die "Could not open export file: $!"; foreach(@export){ print EXP; } close(EXP);

This bit of code is a lot wordier than it needs to be and can be consolidated into few lines.  However, when starting out I think its better to spread the code out to keep your sanity and to make learning and trouble-shooting easier.

-Spenser

That's Spenser, with an "s" like the detective.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://187514]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found