Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql and database functions

Status
Not open for further replies.

abovebrd

IS-IT--Management
May 9, 2000
690
US


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






 
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.
 
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
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
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.
 
In one of the Web Techniques articles, a guy wrote an article on how to make a database independant API in PHP.

It's pretty neat.

You can go here to learn about it, I think that it is much like a watered down version of the Perl DBI.


Thanks,

-Vic
vic cherubini
malice365@hotmail.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top