Search
Left Quote    I have not failed. I've just found 10,000 ways that won't work.
- Thomas Alva Edison    
Right Quote
 
[login] | [Register]
Forum Index » Site and Forum Bugs » Viewing Topic and Replies
Post Reply


        Viewing Topic: Fixed Blank Quote - Code Available
  This user is offline  bs0d
  Subject: "Fixed Blank Quote - Code Available" Posted: @ 1:35 am on Apr 08 2009   

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

I noticed now and then I had an empty quote appear. It was because I was using a random number with rand() between 1 and the max # of rows in the table, then query the quote corresponding to the result of rand().

I just had an auto_increment ID field for an ID number. But as it turns out, I deleted a quote at some point in time and #15 no longer existed, so no quote would appear.

I added a count column to query by since the auto_increment value will always generate a new number. I also optimized the code a bit.

Heres the code I use to generate a random quote. A person should be able to use the same method on their own site if you create a quotes table with the fields: id, count, quote, author:

Code:

<? //get max # of quotes:
$q_count = mysql_query(&qu ot;SELECT count(*) as rows FROM `quotes`") ;
if(!$q_count) die('Error retrieving quotes.');
while ($row = mysql_fetch_ass oc($q_count)) {
$max = $row["rows "];
} //end while

//generate random number within limit of max quotes:
$random_quote = rand(1, $max);

//get quote
$q = mysql_query(&qu ot;SELECT count, quote, author FROM `quotes` WHERE `count` = ".$random_ quote."&qu ot;);
if(!$q) die('Error retrieving quotes.');

while ($quote = mysql_fetch_obj ect($q)) {
echo($quote-> ;quote);
} ?>





-bs0d | AllSyntax.com
    Viewed: 7,550 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: Fixed Blank Quote - Code Availa..." Posted: @ 6:26 pm on Apr 09 2009    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

you can also have mysql pick a random record for you with a query like this:

SELECT count, quote, author FROM `quotes` order by rand() limit 1

i used that sort of thing to show a random animal on every page of the animal shelter site.




please note that the above post is likely made up in its entirety.

    Viewed: 7,534 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Fixed Blank Quote - Code Availa..." Posted: @ 2:47 pm on Apr 10 2009    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

  misterhaan said...

< i> you can also have mysql pick a random record for you with a query like this:

SELECT count, quote, author FROM `quotes` order by rand() limit 1

i used that sort of thing to show a random animal on every page of the animal shelter site.


That's interesting, and probably the fastest route. But when used within an SQL statement, will rand() generate a number within the numbers available in the table?




-bs0d | AllSyntax.com

    Viewed: 7,524 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: Fixed Blank Quote - Code Availa..." Posted: @ 3:07 pm on Apr 13 2009    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

  bs0d said...

< i> But when used within an SQL statement, will rand() generate a number within the numbers available in the table?

mysql rand() always gives you a number between 0 and 1 (including 0, but not 1). using order by rand() with no where clause assigns a random number between 0 and 1 to each row, then sorts by that



Edited at 03:16:43 pm on 04/13/09


please note that the above post is likely made up in its entirety.

    Viewed: 7,498 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank