Lunarpages Web Hosting Forum

Author Topic: Random Item script  (Read 6433 times)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #15 on: March 07, 2004, 09:10:02 PM »
Quote

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


LoL, silly me.  I know what you mean.  If I called the script in a url in my browser.  Yes, I just did it and it says:

Quote

The server encountered an internal error or misconfiguration and was unable to complete your request.


But that is becuase I don't have this script creating a html page.

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 #16 on: March 07, 2004, 09:15:59 PM »
Where is the script located on your server? Try running it from your browser. The output that it shows there will be the same as inserted on the page..

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Random Item script
« Reply #17 on: March 07, 2004, 09:33:13 PM »
the 500 error means that permissions aren't set write or there is an error in the code.

Check your error log and try chmod'ing to 755

- Ed

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #18 on: March 07, 2004, 09:36:03 PM »
How can I do a check to see if it is connecting? The reason I ask is, just to test something, I put in a fake username and pasword and I still ended up with a blank page.  Sense I used a wrong user and pass, it should have errored about not connecting.

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 #19 on: March 07, 2004, 09:43:49 PM »
You could put in print statements throughout the code:

print "About to connect\n";

connect code

print "Connected as user $user\n";

etc..

- Ed

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #20 on: March 07, 2004, 09:49:33 PM »
Thanks for the info.  I put a before connect, after connect, and disconect.  The page shows all three but doesn't show any thing else.  So I'm guessing that the problem lies with reading the database and printing the results of the database.

Thanks again.
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 #21 on: March 07, 2004, 10:01:47 PM »
Make sure that your cases are all right (case sensitive).. and that there is actual data to read! :-)

Good luck with it!

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #22 on: March 07, 2004, 10:04:25 PM »
Ok I got something to work.  Now it prints the whole contents of the database.  What I did to make it work was, change "select" and "from" to "SELECT" and "FROM". So now I am working on trying to get it to select one row at random.  Here is my script so far:

Quote

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

use DBI;

print "Content-type:text/html\n\n";
print "Before connection.\n";

$dbhost = "localhost";
$dbname = "erotic6_items";
$dbtable = "Item_Rotate";
$dbuser = "name";
$dbpass = "pass";

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

srand(time() ^ ($$ + ($$ << 15)) );

$sth = $dbh->prepare("SELECT * FROM $dbtable") 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;
print "Disconnected from database.\n";

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


Thanks for all the help.  If you know anything about randomness. Please let me know.

Thanks again for all your help and time,
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 #23 on: March 07, 2004, 10:08:12 PM »
search on google for some tutorials for mySQL in perl. There are lots of awesome tutorials out there! :-)

- Ed

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #24 on: March 07, 2004, 10:30:06 PM »
Quote from: MagicMike304
Ok I got something to work.  Now it prints the whole contents of the database.  What I did to make it work was, change "select" and "from" to "SELECT" and "FROM". So now I am working on trying to get it to select one row at random.  Here is my script so far:

Quote

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

use DBI;

print "Content-type:text/html\n\n";
print "Before connection.\n";

$dbhost = "localhost";
$dbname = "erotic6_items";
$dbtable = "Item_Rotate";
$dbuser = "name";
$dbpass = "pass";

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

srand(time() ^ ($$ + ($$ << 15)) );

$sth = $dbh->prepare("SELECT * FROM $dbtable") 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;
print "Disconnected from database.\n";

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


Thanks for all the help.  If you know anything about randomness. Please let me know.

Thanks again for all your help and time,
Mike


The uppercase "SELECT" and lowercase "select" is treated equally in SQL. I believe the no output was getting "FROM items" and the one working says "FROM $dbtable" where:

Code: [Select]

$dbtable = "Item_Rotate";


The code should still work with upper- or lowercase letters :thumb:
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #25 on: March 07, 2004, 10:36:30 PM »
Thanks Ripta for pointing that out.  That is the item I also changed.  This is my first time writing a script, and as I see, I have my many steps to take.  It would have been easier to use a flat file DB, but I was wanting to us mySQL.  I have learned alot just today with this script.  I am now going to work on randomness and then I will try ti figure out how to print the out in the format I want.  Thanks for all the help.

Thanks again,
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 #26 on: March 07, 2004, 10:42:56 PM »
For randomness:

Code: [Select]

my $index = int(rand($sth->rows));
my $row;
if ($index) {
  for (my $counter = 0; $counter < $index; $counter++) {
    $row = $sth->fetchrow_hashref;
  }
}


After that code, if all goes well,

Code: [Select]

$row->{SKU}    # will contain the SKU
$row->{Image} # will contain the image

# etc


Of course, the things between { and } are case sensitive. (i.e. if your MySQL database has the column SKU, then you should put SKU. If it has the column sku, put down sku.)

From where I stand, the code *should* work... :D
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Random Item script
« Reply #27 on: March 07, 2004, 10:44:20 PM »
Quote from: MagicMike304
Thanks Ripta for pointing that out.  That is the item I also changed.  This is my first time writing a script, and as I see, I have my many steps to take.  It would have been easier to use a flat file DB, but I was wanting to us mySQL.  I have learned alot just today with this script.  I am now going to work on randomness and then I will try ti figure out how to print the out in the format I want.  Thanks for all the help.

Thanks again,
Mike


Well, MySQL is a step over flat files, but it's really useful in the long run. :thumb: Good luck!
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Random Item script
« Reply #28 on: March 07, 2004, 10:47:44 PM »
Thats pretty admirable goign straight to SQL - but you wont turn back! :-)

I'm off for the night but Ripta (brand new staff) is doing a great job already :-) So I'll just leave it up to him!

- Ed

Offline MagicMike304

  • Intergalactic Superstar
  • *****
  • Posts: 142
    • Katmasters
Random Item script
« Reply #29 on: March 08, 2004, 07:42:15 AM »
Ripta,  Thanks for the information on randomness.  I have been searching and unable to find information on it.  In my book, this is what they tell me:

srand() ^ ($$ + ($$ << 15)) );

$rand1 = rand(100);

and in the sample script for a rand. image they use:

$img = $imgs[int(rand(@imgs))];

I will try the peices of code to posted.  Thanks very much.  I was wondering about using the hashref. Thanks.  Today is my birthday so I may not get to edit my script until later on.  Thanks so much.  You have been a big help.

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