×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

mysql and database functions

mysql and database functions

mysql and database functions

(OP)


I am new to perl. But learning quick

does it seem possible to create a perl (cgi)script that draws data from a mysql database and displays that output in html.

Would perl be well suited for this task ?

Can someone point me some web links that may be usefull

thanks

-Danny
 
 


RE: mysql and database functions

As the others have said, Perl is ideal for this type of thing.  I've done many successful projects using Perl and MySQL.  I'll go into some detail here.

First, make sure you get the DBD module for MySQL from CPAN, in addition to the DBI module.

Then, in your script you want to do something like this:

# database API
use DBI;

# Database Variables
my $db_type = 'mysql'; # RDMS controlling the database
my $db_database = 'name'; # name of the database
my $db_username = 'user'; # username to access this database
my $db_password = 'pass'; # password to access this database

# Connect to your database
my $drh=DBI->install_driver($db_type);
my $dbh=$drh->connect($db_username,$db_database,$db_password) || die "Error connecting to database: $DBI::errstr\n";

# Do a select with @array holding the result
my $selecttable = $dbh->prepare("SELECT fields FROM table WHERE condition");
$selecttable->execute || die "Could not query database: $DBI::errstr.";
my @array = $selecttable->fetchrow;
$selecttable->finish;

# Do an insert
my $insertion = $dbh->prepare("UPDATE table SET field=value WHERE conditions");
$insertion->execute || die "Could not insert: $DBI::errstr.";
$insertion->finish;

# Et cetera.  You can do lots of stuff through the DBI
# module, though I find it easier to actually build my
# database in the mysql terminal program.  I just populate
# it and query it dynamically with my website.

# At the end of your script, you have to
$dbh->disconnect;


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: mysql and database functions

Being quite new to PHP and fairly used to Perl, I would have to disagree by saying that Perl is the best job at this. While Perl is certianly one of the best languages, I think that PHP is best suited for mySQL interactivity.

For example, to connect to a mySQL dbase in PHP, you would use the mysql_connect() or mysql_pconnect() functions, whereas in Perl, you have to use the DBI.

Thats just what I think. Like I said, Perl is a great language, just not the best for this job, in my opinion.

-Vic


vic cherubini
malice365@hotmail.com
www.epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====

RE: mysql and database functions

Yes, but what Perl DBI gives you is portability, which is something that PHP doesn't give you.  If you write your Perl code to the DBI spec and keep your SQL along "standard" lines, your code will be portable even if you change your database from MySQL to Oracle(or whatever) - maybe with a few minor changes.  Doing the same switch in PHP, you'd have to change a lot of your code - right from that start as you can see with the MySQL specific "mysql_connect" or "mysql_pconnect".

I'm arguing your point about PHP just to give a more well-rounded view of the 2 options(PHP vs. Perl DBI) - if code portability is not a concern, then I agree(with a very slim margin) with "vikter" that PHP is more well designed for web page interaction with databases, although I'm really on the fence with this one.  I really like using Perl DBI, but I also like PHP.  PHP gives you "more for nothing" - it was designed from the ground up to be a web programming language, and to make that as easy as possible - but it does NOT give you the portability that Perl DBI does.

Hope I didn't just muddy the water more.


Hardy Merrill
Mission Critical Linux, Inc.
http://www.missioncriticallinux.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close