sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

This is my first time working with a database and I was wondering if anyone would help me get on the right track. Below is my attempt and as you can probably see nothing works. Can anyone point me in the right direction?

#!/usr/bin/perl -w use strict; use warnings; use Mysql; use CGI qw/:standard/; my $query = CGI->new; my %form = $query->Vars; my $entries = "entries.txt"; my $dbh = Mysql->connect($poemDB, sulfericacid, ----); $dbh->selectdb($poemDB); print "Database content\: $dbh"; my $rc = $dbh->shutdown(); if ($form{'poemtitle'} && $form{'poemsub'}) { open (ENTRIES, ">> $entries") or die "Cannot open $entries"; print ENTRIES "$form{'poemtitle'}\n"; print ENTRIES "$form{'poemsub'}\n"; close (ENTRIES); print header(), start_html('Success'), "Your poem entitled <b>$form{'poemtitle'}</b> has been added\n", end_html(); } else { print header(), start_html('Poetry Submissions'), ######### start_form, "Submission Title",textfield('poemtitle'),p, "Submission",textarea(-name=>'poemsub', -rows=>10, -columns=>50), submit, end_form, hr, end_html(); }


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
•Re: My first MySQL database
by merlyn (Sage) on Feb 10, 2003 at 19:03 UTC
    As long as you are starting from scratch, why don't you start with PostgreSQL rather than MySQL? That way, you'll get a real database with full database features.

    Yes, I don't fault anyone who picked MySQL over PostgreSQL two years ago, but the choice has obviously swung in the opposite direction lately.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Why would you suggest a different database? I was just told to use MySQL a few days ago (too many people saying too many different things, lol).

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
        Because PostgreSQL is a baby Oracle, with views, transactions, triggers, nested subselects, full SQL92 datatypes, etc etc. MySQL is now playing "catch up" with PostgreSQL, and I'm not clear that it will ever finish catching up, because it might have to break compatibility.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: My first MySQL database
by l2kashe (Deacon) on Feb 10, 2003 at 19:04 UTC
    I generally use DBI as opposed to a specific DBI variant for database work. With that being said.

    1) Where is $poemDB intialized at?
    2) You never confirm database connection
    3) Since you are only using 1 table from what I can see, why not directly connect to that db and table, as opposed to segregating into 2 lines?
    4) You need to query the DB and return the results ala
    # # Syntax is off I know, but I want to make the point # and allow the creator to find correct syntax as an # exercise # $query = 'SELECT * from $table'; $ret = $dbh->execute($query); for ( fetchrow($ret) ) { print "$_\n"; }
    5) I don't see you adding anything back into the DB, is this intentional or was the code left out?

    Thats all I can see at first glance, but it should be enough to get you rolling forward with this.

    On a side note maybe you should segregate this from a CGI and work a a strict command line level to get a feel for interfacing with the database, and work out the functionality you want/need first. That was you will have a better feel for how you want to present data to the client through the browser..


    /* And the Creator, against his better judgement, wrote man.c */
      Since I haven't a clue on databases and this is my first try I just wanted to see if I could connect and print the contents on the DB. Once I figure that out I'll start writing to them.

      Thanks for your suggestions!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: My first MySQL database
by jobber (Sexton) on Feb 10, 2003 at 18:51 UTC
    Hi, One thing I noticed right off was that you do not confirm if you connect to the database or not. Add this type line to your code when you are dealing with the database handle.
    my $dbh = Mysql->connect($poemDB, sulfericacid, ----) or die $Mysql::db_errstr;
    Also where is $poemDB declared from, use the -w option to see if it has been declared when you use it. That should give you a start. Also make sure you quote "sulfericacid" as a string too.
    Hope this helps and good luck
      Thanks for your suggestions jobber!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: My first MySQL database
by cfreak (Chaplain) on Feb 10, 2003 at 19:12 UTC

    I'm not familier with the Mysql module. I'd suggest checking out DBI. DBI is an abstraction layer that allows you to connect to several different popular databases. Here's a quick example on using it with MySQL:

    #!/usr/bin/perl use strict; use warnings; use DBI; my $dsn = "DBI:mysql:host=somehost;database=db-you-want"; my $dbh = DBI->connect($dsn,"user","password") or die "Couldn't connec +t: " . DBI->errstr(); my $query = "SELECT somestuff FROM sometable WHERE somefield='somethin +g'; my $sth = $dbh->do($query); while(my $somestuff = $sth->fetchrow()) { # do something interesting ... } $sth->finish(); $dbh->disconnect();

    Hope that helps
    chris

    Lobster Aliens Are attacking the world!
      Thanks for your help Chris :)

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: My first MySQL database
by joefission (Monk) on Feb 11, 2003 at 04:55 UTC
    There's a free online book (Simon Cozens' Learning Perl) over at perl.org
    Chapter 13(in pdf) discusses databases and there's a fair chunk that's somewhat MySQL specific. Cheap reference and start for all that fun with perl and databases :-)
Re: My first MySQL database
by sulfericacid (Deacon) on Feb 10, 2003 at 19:17 UTC
    Sorry if this seems to be common knowledge, but what exactly is it looking for in $poemDB?

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid