Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Perl and MySQL - getting a random record

by powerhouse (Friar)
on Feb 05, 2003 at 21:24 UTC ( [id://232935]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have a table that stores image information in it, such as width, height, link, alt(message), and stuff like that.

I added this subroutine that returns the standard format of html to print that "image". here is what I did:

sub Get_Banner_Rotator { my ($row, $dbh, $sth, $html_content); $sth = $dbh->prepare (qq{ SELECT * FROM Banner_Rotator ORDER BY RA +ND() LIMIT 1 }); $sth->execute(); $row = $sth->fetchrow_hashref(); if (defined($row) && $row ne "") { $html_content = qq~Print the link and image~; } $html_content =~ s/{{imageurl}}/$imageurl/g; return($html_content); }

My problem, is that it keeps grabbing the VERY SAME record. Number 1.

Am I missing something here? I do something similar on another one of my websites, and it works, however it is doing the MySQL clause like this:
"SELECT id FROM table WHERE u = '1' ORDER BY RAND() LIMIT 1"

That one works.
of course it is ONLY getting the id from it, but shouldn't mine also grab a random record?

I've only added 3, at this point, but it shows that one only, every single time I refresh the page.

Do you see something wrong there?

thx,
Richard

Replies are listed 'Best First'.
Re: Perl and MySQL - getting a random record
by Coruscate (Sexton) on Feb 05, 2003 at 21:56 UTC

    I get the feeling that perhaps your browser is caching the image that the script returned the first time. Look into adding 'Cache-control', 'Pragma', and/or 'Expires' http headers. Looking at your code even further, where is $imageurl coming from in the Get_Banner_Rotator subroutine? I don't see that declared anywhere... Even worse now... you have a my ($dbh ...); at the top of the sub and then you use it as the database handle. You have to pass the db object into the sub.

    As for the query you have to get a random row from the database, it's correct. I recommend cleaning up the code a bit first. As well, I'm not sure whether you already do or not, but I suspect that your script is missing -w and 'use strict'. Add those on if they are missing. They'll make your life so much easier :)


          C:\>shutdown -s
          >> Could not shut down computer:
          >> Microsoft is logged in remotely.
        

      I do have -w turned on.

      $imageurl, That is defined when the script first executes, what it does is call a subroutine similiar to get_settings(). $imageurl is url() with the /script stripped off. If the page was called through HTTPS, then it is also calling the images with https. then in the Perl code when I am writing a image tag, I put it as src="$imageurl/path/to/image.gif" like that.

      In the databases I put {{imageurl}} then I just switch that with $imageurl.

      Anyways. the current version of MySQL installed is 3.23.54
      I do have another account on this same server that does use RAND() to get a random id which is the name of the image, without the extension. I do that on that site to show sample products out of our database of over 2000 products. it shows 1 on the home page, and it's different every time the page loads.

      In regards to the current site I'm developing, I did clear out my Computers Cache before I reaload it a few times, but it was still record 1. I reloaded the page about 18 to 20 times. Over and over, often clearing the cache first. I also have IE set to check for new pages with every visit.

      I'll add the expires to it to see if that helps. Just in case.

      Thank you for your advice I really DO appreciate it.

      thx,
      Richard
Re: Perl and MySQL - getting a random record
by Cody Pendant (Prior) on Feb 05, 2003 at 22:07 UTC
    Three quick thoughts:
    • Which version of mySQL? ORDER BY RAND() only works in 3.23 and up.
    • I work on a site where this kind of thing is used and it appears that the RAND() is not very random at all in the current version of mySQL. It "prefers" the first record in the table to a very obvious degree
    • Random is random. Picking one out of three is one of three possible outcomes -- who's to say it's wrong? How many times did you try?

    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: Perl and MySQL - getting a random record
by tstock (Curate) on Feb 06, 2003 at 06:51 UTC
    I agree this has nothing to do with perl... and Cody's post got me thinking. try
    SELECT *, rand() as rand FROM Banner_Rotator ORDER BY rand LIMIT 1
    tstock
Re: Perl and MySQL - getting a random record
by Abigail-II (Bishop) on Feb 05, 2003 at 23:48 UTC
    Does MySQL actually garantee any reasonable behaviour when doing ORDER BY RAND()? C's qsort routine requires its comparison function to behave, and so does Perl.

    What also might be happening is that MySQL has actually cached the results - if on a later, identical query the table content hasn't changed, it might actually serve the answer from the cache. But that's just speculation from my side.

    Of course, this question has absolutely nothing to do with Perl.

    Abigail

      Of course, this question has absolutely nothing to do with Perl

      Oh, sorry, I thought it was in a Perl script inside a Perl subroutine. I guess I was mistaken... LOL

      thx,
      Richard
        Care to point out the Perl issue then?

        Abigail

        A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://232935]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found