Lunarpages Web Hosting Forum

Author Topic: Random Item script  (Read 6504 times)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« on: March 07, 2004, 02:41:14 PM »
I am new to Perl/CGI and trying to write my first script.  I am using ssi to run this cgi script.  I want the script to display a random item from the DB. This is just my first step.  After I get this to work, then I will wrok on how and where I want it displayed.  Basicly in this script I have two problems, 1. connecting to my DB and 2. have it random select the item and display it.  Like I said, this is my first time writing.

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

use DBI;

$dbhost = "localhost";
$dbname = "erotic6_items";
$dbtable = "Item_Rotate";
$dbuser = "name";   #This will be sub for my user name
$dbpass = "password";   #This will be sub for my password

$dbh = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost",$dbuser,$dbpass) or dienice("Can't connect: ",$dbh::errstr);

print "Content-type:text/html\n\n";
srand(time() ^ ($$ + ($$ << 15)) );

$sth = $dbh->prepare("select * from items") or dienice("Can't select from table: ",$dbh->errmsg);
$sth->execute;
while (($SKU,$Name,$Price,$Image,$Link) = $sth->fetchrow_array) {
print "$SKU - $Name - $Price - $Image - $Link\n";
}

$dbh->disconnect;

sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}


I know that I could find many free scripts, but I would like to write this to better learn it.  If anyone has any suggestions, it would be appreciated.


Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Re: Random Item script
« Reply #1 on: March 07, 2004, 06:27:27 PM »
Quote from: MagicMike304
I am new to Perl/CGI and trying to write my first script.  I am using ssi to run this cgi script.  I want the script to display a random item from the DB. This is just my first step.  After I get this to work, then I will wrok on how and where I want it displayed.  Basicly in this script I have two problems, 1. connecting to my DB and 2. have it random select the item and display it.  Like I said, this is my first time writing.
Code: [Select]

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

use DBI;

$dbhost = "localhost";
$dbname = "erotic6_items";
$dbtable = "Item_Rotate";
$dbuser = "name";   #This will be sub for my user name
$dbpass = "password";   #This will be sub for my password

$dbh = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost",$dbuser,$dbpass) or dienice("Can't connect: ",$dbh::errstr);

print "Content-type:text/html\n\n";
srand(time() ^ ($$ + ($$ << 15)) );

$sth = $dbh->prepare("select * from items") or dienice("Can't select from table: ",$dbh->errmsg);
$sth->execute;
while (($SKU,$Name,$Price,$Image,$Link) = $sth->fetchrow_array) {
print "$SKU - $Name - $Price - $Image - $Link\n";
}

$dbh->disconnect;

sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}


I know that I could find many free scripts, but I would like to write this to better learn it.  If anyone has any suggestions, it would be appreciated.


Thanks,
Mike


Hi Mike :)

Are you getting errors when connecting to the database? I think the script is already well written. And maybe add:

Code: [Select]

$sth->finish();


before

Code: [Select]

$dbh->disconnect();


:thumb:
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #2 on: March 07, 2004, 06:32:38 PM »
Hello and thanks for responding. I was getting an error that said "Can't Connect" , but I searched the forums here and made a few changes to what the post said.  Now, I just get a blank page.  The page I am testing this on is blank to begin with, but it should have printed something that it fetched from the DB.  Sense I don't get a "Can't Connect", I am assuming that it is connecting.  So, if it is connecting, then now my problem is, it isn't fetching any data or printing it.

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #3 on: March 07, 2004, 06:39:17 PM »
I added

$sth->finish();

But still the same, blank page.

I am not seeing any errors in my error log either.

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #4 on: March 07, 2004, 06:44:09 PM »
Quote from: MagicMike304
Hello and thanks for responding. I was getting an error that said "Can't Connect" , but I searched the forums here and made a few changes to what the post said.  Now, I just get a blank page.  The page I am testing this on is blank to begin with, but it should have printed something that it fetched from the DB.  Sense I don't get a "Can't Connect", I am assuming that it is connecting.  So, if it is connecting, then now my problem is, it isn't fetching any data or printing it.

Thanks,
Mike


Just wondering, what is the SSI tag you're using? Is it something like:

Code: [Select]

<!--#include virtual="/cgi-bin/your-perl-script.pl"-->


But I guess since there was an output before this, it should work. Maybe if I ponder more...
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #5 on: March 07, 2004, 06:44:16 PM »
I'm sorry, I did get a error in my error log

malformed header from script. Bad header=<h2>Error</h2>:
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #6 on: March 07, 2004, 06:46:23 PM »
I am using

<!--#exec cgi="/cgi-bin/mrt.cgi"-->

in a .shtml page.

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #7 on: March 07, 2004, 07:08:20 PM »
I tried changing my SSI to what you posted above and I get this

[an error occurred while processing this directive]

I was thinking the "include" element would just copy what I had in the file.  That is why I was using the "exec" element.  I thought it would "exec" the script.

Thanks for your help,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #8 on: March 07, 2004, 07:12:45 PM »
Quote from: MagicMike304
I tried changing my SSI to what you posted above and I get this

[an error occurred while processing this directive]

I was thinking the "include" element would just copy what I had in the file.  That is why I was using the "exec" element.  I thought it would "exec" the script.

Thanks for your help,
Mike


:thumb: So did it finally work, though?
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #9 on: March 07, 2004, 07:15:29 PM »
I changed the ssi to what you said and I get this printed on the page:

[an error occurred while processing this directive]

and this in my error log:

unable to include "/cgi-bin/mrt.cgi" in parsed file

I can say, at least it did something.  That's better then a blank page, but still no data.

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #10 on: March 07, 2004, 07:16:52 PM »
LOL.
Hmm.. that is weird.
GetAFreelancer! (This service is not affiliated with Lunarpages)


Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #12 on: March 07, 2004, 08:47:01 PM »
Using the exec command just gives me a blank page.  I'm thinking I have the "print" wrong and that's why nothing is showing.

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Random Item script
« Reply #13 on: March 07, 2004, 08:56:59 PM »
You dont get any errors though?

Does the script output fine if you run it on its own?

- Ed

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #14 on: March 07, 2004, 09:06:20 PM »
Quote

Does the script output fine if you run it on its own?


How would I do this?  I am using a ssi to insert the output on a shtml page.

The page is blank and there is a error in my error log that says:

Quote

malformed header from script. Bad header=<h2>Error</h2>:


That's it.  Not sure why it isn't printing on the page.  If I view the page and then view the source, there is nothing in the source either.

Thanks,
Mike
Practice C.P.R. on big catfish - Catch, Photo, Release!
http://www.katmasters.com