Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

MySQL with Perl

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

maddog39
Newbie


Posts: 27
Group: Registered
Joined: Jul 2007
Status: Offline
Reputation: 1
Points: 110 (Donate)
Post: #1

MySQL with Perl


Hello all,

This tutorial should give you a brief understanding on how to use MySQL with Perl. Please note that I will assume that you already have basic knowledge on how to setup databases and that sort of stuff. So the first thing we want to do is of course, create a table. You can use phpMyAdmin or whichever tool you like to add the table and some additional rows, but your going to want to run the following SQL code against your new database.

Code:
CREATE TABLE `perl_articles` (
        `id` INT(255) NOT NULL AUTO_INCREMENT,
        `title` VARCHAR(255) NOT NULL,
        `content` TEXT NOT NULL,
        PRIMARY KEY(`id`)
);
INSERT INTO `perl_articles`(`title`,`content`) VALUES(`test article 1`, `this is some test content...`);
INSERT INTO `perl_articles`(`title`,`content`) VALUES(`test article 2`, `this is some more test content...`);
INSERT INTO `perl_articles`(`title`,`content`) VALUES(`test article 3`, `this is yet again, some more test content...`);

Okay good, now we can get to the good stuff, the perl code. We will just do a simple loop through all the records in the database to show all the "perl_articles".

PHP Code:
#!/usr/bin/perl -wT

use CGI qw(:standard);
use 
CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use 
Mysql;

# Print content headers and opening HTML tags
print header;
print 
start_html("Articles");

# Connect to the database and query information
$db Mysql->connect("localhost""database""username""password");
$query $db->query("SELECT * FROM perl_articles");

# Check if there are actually any news articles
if ($query->numrows 0) {
    
# Loop through all the results or records in the database
    
while (%result $query->fetchhash) {
            
printf "<p>ID: %s<br/>"$result{'id'};
            
printf "Title: %s<br/>"$result{'title'};
            
printf "Content:<br/>%s</p>"$result{'content'};
   }
} else {
    
# If there arent any articles in the database
    
print "<p>Sorry, but no articles currently exist.</p>";
}

# Close HTML tags and finish up
print end_html

The comments should be self explanitory. If you want to see a working version, you can at the link below. Hope you found this tutorial helpful.

Demo: http://www.dx-h.uni.cc/cgi-bin/listarticles.pl

-maddog39

This post was last modified: 25-07-2007 12:15 PM by maddog39.

25-07-2007 12:11 PM
Find all posts by this user Quote this message in a reply
ivenms
Administrator
*******


Posts: 2,179
Group: Administrators
Joined: Sep 2006
Status: Offline
Reputation: 14
Points: 4389 (Donate)
Post: #2

RE: MySQL with Perl


What is Mysql ? I mean that used on the program.
Is that a constant used in the PERL?

I can see the usage of it as Mysql->connect().
Then it is not else were used on the program. From there $db is used.

Also it is better if you give the explanation of $query->fetchhash

Thanks for the wonderful tutorial. Smile


Read: General Rules & Policies before posting.
Make Money By Posting | Earning and Exchanging Points | Add Your Links
25-07-2007 03:27 PM
Find all posts by this user Quote this message in a reply
maddog39
Newbie


Posts: 27
Group: Registered
Joined: Jul 2007
Status: Offline
Reputation: 1
Points: 110 (Donate)
Post: #3

RE: MySQL with Perl


No, Mysql is a Perl Module or class that defines the core mysql functions and implements them. What Im doing with $db is I am storing the information returned by Mysql->connect() which the Mysql database object, you would need an understanding of object oriented programming to really fully understand this. But I take that and use the Mysql object's child functions from my variable $db to perform actions on the database.

[Edit]
Concerning $qeury. When you call a query on the database (as far as I know) the perl module will go fetch your currently instantiated connection information and return a new object, a query object, and that qeury object has different child variables, in my case fetchhash, which would be the equivalent to fetch_array in PHP as well as numrows.

This post was last modified: 27-07-2007 08:33 AM by maddog39.

27-07-2007 08:30 AM
Find all posts by this user Quote this message in a reply
ivenms
Administrator
*******


Posts: 2,179
Group: Administrators
Joined: Sep 2006
Status: Offline
Reputation: 14
Points: 4389 (Donate)
Post: #4

RE: MySQL with Perl


Thanks for the informations given. Smile


Read: General Rules & Policies before posting.
Make Money By Posting | Earning and Exchanging Points | Add Your Links
27-07-2007 05:26 PM
Find all posts by this user Quote this message in a reply
walsh
Senior Member
****


Posts: 401
Group: Registered
Joined: Oct 2006
Status: Offline
Reputation: 0
Points: 680 (Donate)
Post: #5

RE: MySQL with Perl


I am reading first time about the perl.

I know that perl is also like the CGI or PHP languages. Perl files normally seen on .pl extension. But don't have much knowledge about perl language.

What is the extension of PERL?


09-08-2007 07:04 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Simplest Perl CGI geekyone 1 270 15-04-2008 04:31 PM
Last Post: ivenms
Smile Testing Perl Programs bomber 0 631 19-09-2006 12:39 AM
Last Post: bomber
Smile Storing and Running Perl Programs bomber 0 691 19-09-2006 12:13 AM
Last Post: bomber
Smile Taste of Perl bomber 0 674 19-09-2006 12:11 AM
Last Post: bomber
Smile State of Perl bomber 0 696 19-09-2006 12:09 AM
Last Post: bomber
Smile What Is Perl? bomber 0 649 19-09-2006 12:07 AM
Last Post: bomber

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 08-10-2008, 12:59 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB